首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何遍历excel中的列并在python中操作它?

如何遍历excel中的列并在python中操作它?
EN

Stack Overflow用户
提问于 2018-08-09 06:01:48
回答 1查看 0关注 0票数 0

我有2个excel xlsm文件。第一个文件我将2列数据合并到一个字典中,我想将该字典的键与第二个文件中的数据列表进行比较。如果来自两个数据的密钥和“代码”都是等价的,那么“代码变得更改为与密钥配对的值。我不知道如何执行此过程我所做的全部是制作字典并对它们进行比较。

代码语言:javascript
复制
import itertools #module that includes functions to work with iterable data
import pandas as pd # this allows for data manipulation and analysis to and 
from excel
from pandas import ExcelWriter
from pandas import ExcelFile
import pickle # its a module that publishes objects or other data so they 
can be saved to a file and opened again later
import os.path # allows to work/manipulate on file names and paths
import xlrd # A module that allows python to read excel files
import xlwings as xw # module that helps to tie python, excel, and pandas 
to take data from a database, manipulate it and present it in a spreadsheet
from tkinter import * # standard GUI
from tkinter.filedialog import askopenfilename # module with open and save 
functions
import re # a module that helps to match or find sets of strings
import datetime 

#Accessing the ticker col
print("Accessing PerfData")
performancefilename = r"C:\Users\opera\Documents\Evan 
Projects\matrix\PerfData.xlsm"
data = xlrd.open_workbook(performancefilename)
sheet = data.sheet_by_index(0)
  #Storing ticker values in colTicker
colTicker = sheet.col_values(1, 1, 6000)


#Accessing AA and Z list from Master List
print("Accesing nessecary values from the Master List")
bondfilename = r'C:\Users\opera\Documents\Evan Projects\matrix\P&L 
061118.xlsm'
book = xw.Book(bondfilename)
sheet = book.sheets['Master List']
translateFrom = sheet.range("Z2:Z1500").value
translateTo = sheet.range("AA2:AA1500").value

#Pairing Z and AA list in a dictionary
oldNew = {}
for From, To in zip(translateFrom, translateTo):
    oldNew[From] = To

#Comparing master list and ticker col

for key in oldNew:
    if key in colTicker:
        print (colTicker)
EN

回答 1

Stack Overflow用户

发布于 2018-08-09 15:11:42

参阅以下内容:

代码语言:javascript
复制
dict = {'name': 'alan', 'place': 'memphis', 'food': 'pizza'}
tickers = ['name', 'place', 'ugly']

for key, value in dict.iteritems():
    for n in tickers:
        if key == n:
            x = n.replace(n, value)
            print(x)

输出:

代码语言:javascript
复制
memphis, alan
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档