[英]TypeError: list indices must be integers or slices not str
I cant get this to work, I have read other SO qs regarding this issue but I cant seem to find the info to suit my application.我不能让它工作,我已经阅读了关于这个问题的其他 SO qs,但我似乎找不到适合我的应用程序的信息。 Im getting
TypeError: list indices must be integers or slices not str
Im holding my with open
in a function as I only want to call it when a button is pressed.我得到
TypeError: list indices must be integers or slices not str
我在 function 中保持我with open
,因为我只想在按下按钮时调用它。 Im sure it has to do with for row in csv_file:
do I need a for line in csv_file:
then for row
?我确定它与
for row in csv_file:
我是否需要for line in csv_file:
然后for row
? Im stuck我卡住了
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()
Here is the error RoNumber = row['Ro Number']
这是错误
RoNumber = row['Ro Number']
Here is some.csv这是一些.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
You're accessing the row columns by field name, not index.您正在按字段名称访问行列,而不是索引。 To use field names, try the
DictReader
function.要使用字段名称,请尝试使用
DictReader
function。
csv_file = csv.DictReader(csv_file)
You're also reusing a variable name which may be causing issues.您还重用了可能导致问题的变量名。
Replace this code:替换此代码:
with open(input_file_path) as csv_file:
csv_file = csv.reader(csv_file)
for row in csv_file:
With this:有了这个:
with open(input_file_path) as csv_file:
reader = csv.reader(csv_file)
for row in reader:
This code works for me (it also works using a csv file):此代码适用于我(它也适用于 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: 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
Here is the code using tkinter.这是使用 tkinter 的代码。 It works correctly with the sample data.
它适用于示例数据。
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: 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.