检查出我的Excel 2003中,似乎可以电子表格保存为xml
,txt
,csv
等等。
不知道文本文件的外观如何,但是我会以此为开头,因为这可能会为您节省库导入(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]])
来源
2017-07-25 20:22:22
Jay
对不起,这个,对此还不明白。我已将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)我知道它错了,教程中的例子显示了如何打印,但那不是我所需要的,是否有读取函数? –
请参阅编辑代码片段 – Jay
对于延迟抱歉,没有机会尝试它,但我仍然无法使其工作。我如何将这个应用到选项菜单对象? –