Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python读写错误_Python_Csv - Fatal编程技术网

Python读写错误

Python读写错误,python,csv,Python,Csv,当我运行这段代码时,我得到了这个错误 import csv dataList = [] with open('N004_timetable.csv', 'rb') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: dataList.append[row] csvfile.close() print(dataList[2][2]) with open('N004_timeta

当我运行这段代码时,我得到了这个错误

import csv 
dataList = []

with open('N004_timetable.csv', 'rb') as csvfile:
    csvreader = csv.reader(csvfile)
    for row in csvreader:
        dataList.append[row]

csvfile.close()

print(dataList[2][2])
with open('N004_timetable.csv', 'rt') as csvfile:
我得到了这个错误

import csv 
dataList = []

with open('N004_timetable.csv', 'rb') as csvfile:
    csvreader = csv.reader(csvfile)
    for row in csvreader:
        dataList.append[row]

csvfile.close()

print(dataList[2][2])
with open('N004_timetable.csv', 'rt') as csvfile:
回溯(最近一次呼叫最后一次):
文件“C:\attention\Class.py”,第6行,在
dataList.append[行]
TypeError:“内置函数”或“方法”对象不可下标

我做错了什么?

“你是以文本模式打开文件的吗?”
以open('N004_timeline.csv','rb')作为csvfile
=>
以open('N004_timeline.csv','r')作为csvfile(/code>追加[row]
追加(row)@Jean-Françoisfare谢谢:DSmall细节:你不需要明确关闭文件(
csvfile.close()
)在使用上下文管理器打开它时(
与…
)它会为您处理。您似乎在Python 3中使用Python 2代码。请确保查看正确的文档-例如,在Python 3中,在打开CSV文件时需要设置
换行符
参数)。