IndexError: list index out of range 现在我已经改变了文件的读取方式
Posted
技术标签:
【中文标题】IndexError: list index out of range 现在我已经改变了文件的读取方式【英文标题】:IndexError: list index out of range is thrown now that i've changed the way the file is read 【发布时间】:2018-11-15 04:46:35 【问题描述】:我正在尝试读取并重新格式化一个非常大 (2GB+) 的 .out 文件,该文件的结构类似于 csv。我之前使用的是标准的 open(),没有出现这样的问题,但将其更改为 codecs.open(),因为它遇到了一些字符问题。
现在开始扔了
Traceback (most recent call last):
line 21, in <module>
if(r[5]==""):
IndexError: list index out of range
在第一行,尽管在 r[5] 处肯定有一个元素。
(运行时间为 0.301s)
import sys
import csv
import datetime
import codecs
maxInt=sys.maxsize
decrement=True
while decrement:
decrement=False
try:
csv.field_size_limit(maxInt)
except OverflowError:
maxInt = int(maxInt/10)
decrement = True
with codecs.open("file.out", 'rU', 'utf-16-be') as source:
rdr = csv.reader(source)
with open("out.csv","w", newline='') as result:
wtr = csv.writer(result)
wtr.writerow(("Column1", "column2", "column3", "etc..."))
for r in rdr:
if(r[5]==""):
continue
wtr.writerow((datetime.datetime.strptime(r[5], '%m/%d/%Y').strftime('%Y-%m-%d'), r[3], r[7], r[9]+r[10]+" "+r[12]))
使用 utf-8 抛出 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc9 in position 12: invalid continuation byte
使用 latin-1 或 ISO-8859-1 会抛出 UnicodeEncodeError: 'charmap' codec can't encode characters in position 57-58: character maps to <undefined>
,尽管在运行更多之后。
输入文件如下所示:
"A00017","K","G","1999","4530","01/12/1999","","","","PEOPLE TO ELECT MANGINELLI","","","","258 MAGNIOLIA DRIVE","SELDEN","NY","11784","","","404.57","","","","","","","2","","NAA","07/22/1999 08:43:59"
"A00037","K","G","1999","999999","01/12/1999","","","","CITIZENS TO ELECT TEDISCO TO ASSEMBLY","","","","","","","","","","0","","","","","","","2","","",""
"A00037","K","N","1999","1693","01/15/1999","","","","OUTSTANDING LOAN","","","","2176 GUILDERLAND AVE","SCHENECTADY","NY","12306","","","10474.8","10474.8","","","OTHER","","PREVIOUS LOAN FROM JAMES TEDISCO","","P","JM","07/15/1999 15:08:17"
"A00037","J","N","2000","1694","01/13/2000","","","","OUTSTANDING LOAN","","","","2176 GUILDERLAND","SCHENECTADY","NY","12306","","","10474.8","10474.8","","","OTHER","","LOANS FROM PREVIOUS CAMPAIGNS FROM J","","P","JM","01/14/1900 16:35:09"
"A00037","K","X","2000","999999","","","","","","","","","","","","","","","","","","","","","","","","","07/20/2000 00:00:00"
"A00037","J","X","2001","999999","","","","","","","","","","","","","","","","","","","","","","","","","01/17/2001 00:00:00"
"A00037","K","X","2002","999999","","","","","","","","","","","","","","","","","","","","","","","","","07/19/2002 00:00:00"
"A00037","J","X","2003","999999","","","","","","","","","","","","","","","","","","","","","","","","","01/21/2003 00:00:00"
"A00037","K","X","2003","999999","","","","","","","","","","","","","","","","","","","","","","","","","07/16/2003 00:00:00"
"A00037","J","X","2004","999999","","","","","","","","","","","","","","","","","","","","","","","","","01/22/2004 00:00:00"
感谢:
"Line contains NULL byte" in CSV reader (Python)
_csv.Error: field larger than field limit (131072)
【问题讨论】:
你能在你的循环中打印r
吗?
没有看到重现问题的文件,我们很难为您提供帮助。
【参考方案1】:
在您正在读取的“file.out”中,找出行中每个单元格的元素之间的分隔符。像 '\t'-tab 或 ','-comma 并将其传递给 'delimiter' 属性。
尝试打印 'r' 并查看列名之间的字符或行中的值
rdr = csv.reader(source,delimiter=<separator>)
【讨论】:
你试过打印 'r' 看看它是不是一个数组吗? 尝试将 r 打印到控制台并得到UnicodeEncodeError: 'charmap' codec can't encode characters in position 2-97: character maps to <undefined>
在 codecs.open("file.out", 'rU', 'utf-8') 中使用 'utf-8'
你可以尝试使用'latin-1'或'ISO-8859-1'编码而不是'utf-8'
你能试试这个代码吗:rdr = csv.reader((line.replace('\0','') for line in source),delimiter=',') 你也可以分享导致错误的数据行以上是关于IndexError: list index out of range 现在我已经改变了文件的读取方式的主要内容,如果未能解决你的问题,请参考以下文章
IndexError: List Index out of range Keras Tokenizer
IndexError: list index out of range 现在我已经改变了文件的读取方式
python-爬取中国大学排名网站信息IndexError:list index out of range
python-爬取中国大学排名网站信息IndexError:list index out of range
关于Python一直提示IndexError: list index out of range是怎么回事?
IndexError: list index out of range ,同时在python的列表中查找第一个不连续的数字