DMM*_*MML 3 python windows csv indexing macos
我使用Wing IDE在Mac上开发了一些代码.我开发的代码,它使用csv模块,正在工作,并在我的Mac上做我想要的.但问题是,我编写它的人需要在Windows上使用它.我并不关心代码,因为我没有使用任何转义字符.
代码如下所示:
csvfile = open('file.csv', 'r')
csvreader = csv.reader(csvfile, delimiter = ',')
for row in csvreader:
thisVariable = row[0] # <<<--------
Run Code Online (Sandbox Code Playgroud)
我在上面输入的'箭头'是在Windows机器上返回错误的地方.就像我说的那样,代码在Mac上运行良好,实际上,这在我编写的代码中相当远.在此语句上方读取和写入的其他CSV文件使用类似的索引.
我真的很感激任何人对此问题的看法!谢谢!
在Python 2中
您需要将文件作为二进制文件打开:
csvfile = open('file.csv', 'rb')
csvreader = csv.reader(csvfile, delimiter = ',')
for row in csvreader:
thisVariable = row[0]
Run Code Online (Sandbox Code Playgroud)
http://docs.python.org/2/library/csv.html#csv.reader
在Python 3中
您需要在open语句中设置newline ='':
csvfile = open('file.csv', 'r', newline='')
csvreader = csv.reader(csvfile, delimiter = ',')
for row in csvreader:
thisVariable = row[0]
Run Code Online (Sandbox Code Playgroud)
http://docs.python.org/3.3/library/csv.html#csv.reader