_csv.Error: 字段大于字段限制 (131072) [英] _csv.Error: field larger than field limit (131072)
问题描述
我有一个读取 csv 文件的脚本,其中包含非常大的字段:
I have a script reading in a csv file with very huge fields:
# example from http://docs.python.org/3.3/library/csv.html?highlight=csv%20dictreader#examples
import csv
with open('some.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
但是,这会在某些 csv 文件上引发以下错误:
However, this throws the following error on some csv files:
_csv.Error: field larger than field limit (131072)
如何分析具有巨大字段的 csv 文件?由于需要在后续步骤中分析数据,因此不能选择跳过具有巨大字段的行.
How can I analyze csv files with huge fields? Skipping the lines with huge fields is not an option as the data needs to be analyzed in subsequent steps.
推荐答案
csv文件可能包含非常大的字段,因此增加field_size_limit
:
The csv file might contain very huge fields, therefore increase the field_size_limit
:
import sys
import csv
csv.field_size_limit(sys.maxsize)
sys.maxsize
适用于 Python 2.x 和 3.x.sys.maxint
仅适用于 Python 2.x (SO:what-is-sys-maxint-in-python-3)
sys.maxsize
works for Python 2.x and 3.x. sys.maxint
would only work with Python 2.x (SO: what-is-sys-maxint-in-python-3)
正如 Geoff 所指出的,上面的代码可能会导致以下错误:OverflowError: Python int too large to convert to C long
.为了避免这种情况,您可以使用以下快速而肮脏的代码(它应该适用于使用 Python 2 和 Python 3 的每个系统):
As Geoff pointed out, the code above might result in the following error: OverflowError: Python int too large to convert to C long
.
To circumvent this, you could use the following quick and dirty code (which should work on every system with Python 2 and Python 3):
import sys
import csv
maxInt = sys.maxsize
while True:
# decrease the maxInt value by factor 10
# as long as the OverflowError occurs.
try:
csv.field_size_limit(maxInt)
break
except OverflowError:
maxInt = int(maxInt/10)
这篇关于_csv.Error: 字段大于字段限制 (131072)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!