简体   繁体   English

IndexError:列表索引超出了CSV文件读取python中的范围

[英]IndexError: list index out of range in CSV file reading python

I have a csv file contaning 30000000 entries. 我有一个包含30000000个条目的csv文件。 like this 像这样

കൃഷി 3
വ്യാപകമാകുന്നു 2
നെല്‍കൃഷി 2
വെള്ളം 2
നെല്ല് 2
മാത്രമേ 2
ജല 2

When I try to reverse the word order I am getting the following error 当我尝试反转单词顺序时,出现以下错误

Traceback (most recent call last):
  File "/home//grpus/dg.py", line 8, in <module>
    writer.writerow((row[1], row[0]))
IndexError: list index out of range

This is the code: 这是代码:

import csv

with open('s.csv', 'rb') as f:
    reader = csv.reader(f, delimiter='\t')
    with open("revmal.txt", "w") as o:
        writer = csv.writer(o, delimiter='\t')
        for row in reader:
            writer.writerow((row[1], row[0]))

Edit 编辑

 writer.writerow(row[::-1])

When I try to fix it 当我尝试修复它时

How to fix this error? 如何解决这个错误?

 Traceback (most recent call last):
      File "/home/grpus/dg.py", line 7, in <module>
        for row in reader:
    Error: field larger than field limit (131072)

The file is 1.4 Gb in size 该文件的大小为1.4 Gb

wc -L s.csv

936 936

{if(length($0)>max){max=length($0);maxline=$0}}END{print maxline} This produced 

! 1, 186 characters ! 1,186个字符

You have at least one row that doesn't have 2 columns separated by a tab. 您至少有一行没有两列,且各列之间没有一个制表符。 An empty line, for example, or if your format doesn't actually use tabs. 例如,如果显示为空行,或者您的格式实际上未使用制表符。

You have two options: 您有两种选择:

  1. skip rows with fewer columns than you need: 跳过行数少于所需的行:

     for row in reader: if len(row) < 2: continue writer.writerow((row[1], row[0])) 
  2. fix your delimiter to match the actual file content: 修复分隔符以匹配实际文件内容:

     reader = csv.reader(f, delimiter=' ') 

    you could use the csv.Sniffer() class to try and automate delimiter selection, if you have more than one file to process, and these files are not all following the same CSV dialect. 如果您要处理多个文件,并且这些文件并非都遵循相同的CSV方言,则可以使用csv.Sniffer()尝试自动执行定界符选择。

Since all you want to do is write the file in reverse order, just write the same row back, but in reverse; 由于您要做的就是以相反的顺序写入文件,因此只需将同一行写回,但是要相反。 like this: 像这样:

 writer.writerow(row[::-1])

A negative index starts from the right, and a negative step value (the third argument in the slice syntax) will simply reverse the object. 负索引从右开始,而负步进值(slice语法中的第三个参数)将简单地反转对象。

This will stop the error you are seeing now, and in case you have rows columns that are not 2, they will also be written in reverse. 这将停止您现在看到的错误,并且如果您的行列不是2,则它们也将以相反的方式写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM