我有2个excel xlsm文件。第一个文件我将2列数据合并到一个字典中,我想将该字典的键与第二个文件中的数据列表进行比较。如果来自两个数据的密钥和“代码”都是等价的,那么“代码变得更改为与密钥配对的值。我不知道如何执行此过程我所做的全部是制作字典并对它们进行比较。
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)
发布于 2018-08-09 15:11:42
参阅以下内容:
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)
输出:
memphis, alan
https://stackoverflow.com/questions/-100002045
复制相似问题