我的 python 脚本有什么错误

What is the error in my python script

我有这个 python 脚本可以读取 CSV 文件并将请求的字段附加到 2 个空列表中。

但是系统显示这个错误:

  File "C:\Users\test\Documents\Python_Projects\readCSV.py", line 10, in <module>
  mywind.append(row[8])

builtins.AttributeError: 'tuple' object has no attribute 'append'

但是当我得到 mywind 的类型时,它会显示列表。

代码:

import csv
mydelimeter = csv.excel()
mydelimeter.delimiter=";"
myfile = open("C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv")
myfile.readline()
myreader=csv.reader(myfile,mydelimeter)
mywind=mydate=[],[]
for row in myreader:
    print(row[0],row[8])
    mywind.append(row[8])
    mydate.append(row[0])

theindex=mywind.index(max(mywind))
print(mywind[theindex],mydate[theindex])
myfile.close()

mywind=mydate=[],[] 等同于 mywind = mydate = ( [] , [] ) - 即:mywindmydate 等于包含两个空列表的元组。

>>> mywind = mydate = [] , []
>>> mywind
([], [])
>>> mydate
([], [])

我建议您将其扩展为两行,这是明确的:

mywind = []
mydate = []

使用元组赋值是“聪明”,但在写作和理解上都会出现错误。如果你坚持,这将是正确的:

mywind, mydate = [], []

第 7 行应为 mywind,mydate=[],[](注意逗号而不是等号)。