檢查出我的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
對於延遲抱歉,沒有機會嘗試它,但我仍然無法使其工作。我如何將這個應用到選項菜單對象? –