蟒3.7 64位的Windows 7(64位):CSV - 场比场限值大(131072)

问题描述 投票:0回答:1

_csv.Error: field larger than field limit (131072)不是固定我的问题。

我有一个处理CSV文件导入到Excel报表的脚本。直到一些特定的CSV文件变得非常大(目前> 12 MB)的脚本工作就好了。

该脚本通常运行在Windows上,因为球队7 64位使用的是Windows客户端。 Python版本的范围从3.6到3.7.2 - 所有的64位。所有版本产生错误。

错误我得到第一个地方是

_csv.Error:字段比场限值大(131072)

其中,使用搜索功能,似乎是很容易解决。但是,当我有

csv.field_size_limit(了sys.maxsize)

这使得它只有更糟:

Traceback (most recent call last):
  File "CSV-to-Excel.py", line 123, in <module>
    report = process_csv_report(infile)
  File "CSV-to-Excel.py", line 30, in process_csv_report
    csv.field_size_limit(sys.maxsize)
OverflowError: Python int too large to convert to C long

根据我的研究是错误应该早已固定。

我目前的解决方法是使用Linux在代码只是正常工作。但是应该运行的脚本不能运行Linux,但被锁定在Windows团队。

该脚本的代码

#!c:\python37\python.exe

import csv
import sys


def process_csv_report(CSV_report_file):
    files = []
    files.append(CSV_report_file+"_low.csv")
    files.append(CSV_report_file+"_med.csv")
    files.append(CSV_report_file+"_high.csv")
    first = True
    try:
        report = []
        for f in files:
            if first == True:
                with open(f, "r", newline='', encoding='utf-8') as csvfile:
                    original = csv.reader(csvfile, delimiter=',', quotechar='"')
                    for row in original:
                        report.append(row)
                first = False
            else:
                with open(f, "r", newline='', encoding='utf-8') as csvfile:
                    original = csv.reader(csvfile, delimiter=',', quotechar='"')
                    # for the second and third file skip the header line
                    next(original, None)
                    for row in original:
                        report.append(row)
    except Exception as e:
        print("File I/O error! File: {}; Error: {}".format(f, str(e)))
        exit(1)
    return report


if __name__ == "__main__":
    report = process_csv_report(infile)

由于简单,因为它似乎我在固定的问题,因为该解决方案为别人打工失败,这里没有任何理由,我可以看到丢失。

有没有人作用似乎这与Python的后期版本最近发生的呢?

python csv
1个回答
2
投票

你可以通过sys.maxsize,这是c integer max value取代2147483647

我知道sys.maxsize应该照顾它,但我想用一个值不如屋顶,像1.000.000应该解决您的问题。

一个更好的办法做到这一点可能是min(sys.maxsize, 2147483646)

_csv库是编译的扩展,那么它使用C变量。

© www.soinside.com 2019 - 2024. All rights reserved.