Python CSV - 列表索引超出范围 [英] Python CSV - list index out of range
问题描述
我收到此错误读取CSV文件(无标题,3列,第二和第三个字符串):
I get this error reading CSV file (no headers, 3 columns, 2nd and 3rd strings):
Traceback (most recent call last):
File "C:\Python32\fantasy.py", line 72, in module>
some=row[1]
IndexError: list index out of range*
这里是下面的代码的一部分。这是愚蠢的简单的东西,坚持,但我只是空白,它是如何不工作。我是新的编码,但处理了csv模块之前,从来没有这个部分的问题,只是在记事本中做一些测试csv文件,看看它是否将从相同的代码读取,它是。我不知道。
Here is part of the code below. It's stupidly simple stuff to be stuck on, but I'm just blank about how it isn't working. I'm new to coding, but have dealt with csv module before and never had problems with this part, and just made some test csv file in notepad to see if it will be read from the same code, and it does. I don't know.
import csv
##############some other code, working good and I believe not relevant to this problem
file=open(r"C:\Users\me\Desktop\file-2.csv","r")
reader=csv.reader(file, delimiter=',', quotechar='"')
for row in reader:
some=row[1]
推荐答案
请尝试检查空行,同时避免使用 c>作为变量名。
r
是打开的默认模式。
Try checking for blank lines. Also, avoid using file
as a variable name. "r"
is the default mode with open.
import csv
with open(r"C:\Users\me\Desktop\file-2.csv") as f:
reader = csv.reader(f, delimiter=',', quotechar='"')
for row in reader:
if row:
some=row[1]
这篇关于Python CSV - 列表索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!