_csv。错误:字段大于字段限制(131072)

浏览:55日期:2024-03-05
如何解决_csv。错误:字段大于字段限制(131072)?

csv文件可能包含非常大的字段,因此请增加field_size_limit:

import sysimport csvcsv.field_size_limit(sys.maxsize)

sys.maxsize适用于Python 2.x和3.x。sys.maxint仅适用于Python 2.x(因此:what-is-sys-maxint-in-python-3)

更新资料

正如Geoff指出的那样,上面的代码可能会导致以下错误:: Python int too large to convert toC long。为了避免这种情况,您可以使用以下 快速而又肮脏的 代码(该代码应该在使用Python 2和Python 3的每个系统上都可以使用):

import sysimport csvmaxInt = sys.maxsizewhile 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文件中读取:

# example from http://docs.python.org/3.3/library/csv.html?highlight=csv%20dictreader#examplesimport csvwith open(’some.csv’,newline=’’) as f: reader = csv.reader(f) for row in reader:print(row)

但是,这会在某些csv文件上引发以下错误:

_csv.Error: field larger than field limit (131072)

如何分析具有巨大字段的csv文件?跳过具有巨大字段的行不是一种选择,因为需要在后续步骤中分析数据。

相关文章: