2017-07-25 81 views
1

我有一个选项菜单工作好,从代码列表中查找,但如果列表要在Excel文件中进行外部维护,该怎么办?我将如何去查找一个excel文件,名为.... vehicles.xlsx,列表将在A列?我会使用熊猫还是python中有现有的选项 工作代码在下面,但显然我想摆脱列表并将其替换为excel查找。tkinter optionmenu查找excel列表

对不起,如果有教程的地方,但我似乎无法找到它,我很新的python。

由于

self.cars = [ “CAR1”, “CAR2”] self.installin = tk.OptionMenu(个体,self.var,* self.cars).grid(行= 4,列= 2,粘= tk.W,padx = 8,pady = 8)

回答

0

检查出我的Excel 2003中,似乎可以电子表格保存为xmltxtcsv等等。

不知道文本文件的外观如何,但是我会以此为开头,因为这可能会为您节省库导入(xml,csv),以防万一。

编辑:如果不是,请查看csvs'DictReader,我相信这是最快的解决方案。

如果您喜欢/喜欢使用json,您还可以使用few lines of code将csv转换为json。

编辑2:提取从一个CSV文件中的第一列可能是这样的:

import csv 

with open('install.csv') as csvfile: 

    # Generating a DictReader object 
    reader=csv.DictReader(csvfile) 

    # This will contain the values in the 1st column 
    first_col=[] 

    # Assuming the first column is titled 'Ins' and skipping empty cells 
    first_col.extend([row['Ins'] for row in reader if row['Ins']]) 

另外,如果你没有名字列,你可以只使用csv.reader。相关部分如下所示:

reader=csv.reader(csvfile) # instead of csv.DictReader(csvfile) 

first_col=[] 

first_col.extend([row[0] for row in reader if len(row) and row[0]]) 
+0

对不起,这个,对此还不明白。我已将open('install.csv')添加为csvfile: self.reader = csv.DictReader(csvfile) for self in.reader: read(row ['Ins'])self.installin = tk.OptionMenu(self,self.var,* self.reader)我知道它错了,教程中的例子显示了如何打印,但那不是我所需要的,是否有读取函数? –

+0

请参阅编辑代码片段 – Jay

+0

对于延迟抱歉,没有机会尝试它,但我仍然无法使其工作。我如何将这个应用到选项菜单对象? –