![](/img/trans.png)
[英]“TypeError: list indices must be integers or slices, not str”
[英]TypeError: list indices must be integers or slices not str
我不能让它工作,我已经阅读了关于这个问题的其他 SO qs,但我似乎找不到适合我的应用程序的信息。 我得到TypeError: list indices must be integers or slices not str
我在 function 中保持我with open
,因为我只想在按下按钮时调用它。 我确定它与for row in csv_file:
我是否需要for line in csv_file:
然后for row
? 我卡住了
import tkinter as tk
from tkinter import *
import tkinter.ttk as tkrttk
from PIL import Image, ImageFont, ImageTk
import csv
from tkinter import filedialog
def select_input_file():
input_file_path = filedialog.askopenfilename(filetypes=(("CSV files", "*.csv"),))
with open(input_file_path) as csv_file:
csv_file = csv.reader(csv_file)
for row in csv_file:
RoNumber = row['Ro Number']
DateIn = row['Date In']
TimeIn = row['Time In']
TimeOut = row['Time Out']
RegoNumber = row['Rego Number']
CustomerName = row['Customer Name']
VehicleMake = row['Vehicle Make']
VehicleModel = row['Vehicle Model']
JobDescription = row['Job Description']
CurrentStatus = row['Current Status']
treetime.insert("", 0, values=(RoNumber, DateIn, TimeIn, TimeOut, RegoNumber,
CustomerName, VehicleMake, VehicleModel, JobDescription, CurrentStatus))
root.mainloop()
这是错误RoNumber = row['Ro Number']
这是一些.csv
'Ro Number,Date In,Rego Number,Customer Name,Vehicle Make,Vehicle Model,Job Description,Current Status,Time In,Time Out
123456,6/07/2020,abc123,Conor McGregor,Hyundai ,i30,"15,000 Km Service",,8:00:00 AM,4:00:00 PM
654321,31/07/2020,acb321,Nate Diaz,Ferrari,Enzo,Crank but wont fire,,9:30:00 AM,4:45:00 PM
123456,6/07/2020,abc123,Conor McGregor,Hyundai ,i30,"15,000 Km Service",,8:00:00 AM,4:00:00 PM
您正在按字段名称访问行列,而不是索引。 要使用字段名称,请尝试使用DictReader
function。
csv_file = csv.DictReader(csv_file)
您还重用了可能导致问题的变量名。
替换此代码:
with open(input_file_path) as csv_file:
csv_file = csv.reader(csv_file)
for row in csv_file:
有了这个:
with open(input_file_path) as csv_file:
reader = csv.reader(csv_file)
for row in reader:
此代码适用于我(它也适用于 csv 文件):
from io import StringIO
scsv = '''
Ro Number,Date In,Rego Number,Customer Name,Vehicle Make,Vehicle Model,Job Description,Current Status,Time In,Time Out
123456,6/07/2020,abc123,Conor McGregor,Hyundai ,i30,"15,000 Km Service",,8:00:00 AM,4:00:00 PM
654321,31/07/2020,acb321,Nate Diaz,Ferrari,Enzo,Crank but wont fire,,9:30:00 AM,4:45:00 PM
123456,6/07/2020,abc123,Conor McGregor,Hyundai ,i30,"15,000 Km Service",,8:00:00 AM,4:00:00 PM
'''.strip()
f = StringIO(scsv)
reader = csv.DictReader(f)
for row in reader:
RoNumber = row['Ro Number']
DateIn = row['Date In']
TimeIn = row['Time In']
TimeOut = row['Time Out']
RegoNumber = row['Rego Number']
CustomerName = row['Customer Name']
VehicleMake = row['Vehicle Make']
VehicleModel = row['Vehicle Model']
JobDescription = row['Job Description']
CurrentStatus = row['Current Status']
print(RoNumber,DateIn,TimeIn,TimeOut)
Output:
123456 6/07/2020 8:00:00 AM 4:00:00 PM
654321 31/07/2020 9:30:00 AM 4:45:00 PM
123456 6/07/2020 8:00:00 AM 4:00:00 PM
这是使用 tkinter 的代码。 它适用于示例数据。
import tkinter as tk
from tkinter import *
import tkinter.ttk as tkrttk
from PIL import Image, ImageFont, ImageTk
import csv
from tkinter import filedialog
def select_input_file(x):
input_file_path = filedialog.askopenfilename(filetypes=(("CSV files", "*.csv"),))
with open(input_file_path) as csv_file:
rdr = csv.DictReader(csv_file)
for row in rdr:
RoNumber = row['Ro Number']
DateIn = row['Date In']
TimeIn = row['Time In']
TimeOut = row['Time Out']
RegoNumber = row['Rego Number']
CustomerName = row['Customer Name']
VehicleMake = row['Vehicle Make']
VehicleModel = row['Vehicle Model']
JobDescription = row['Job Description']
CurrentStatus = row['Current Status']
print(RoNumber,DateIn,TimeIn,TimeOut)
widget = Button(None, text='Open CSV')
widget.pack()
widget.bind('<Button-1>', select_input_file)
widget.mainloop()
Output:
123456 6/07/2020 8:00:00 AM 4:00:00 PM
654321 31/07/2020 9:30:00 AM 4:45:00 PM
123456 6/07/2020 8:00:00 AM 4:00:00 PM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.