_csv. 오류 : 필드 제한보다 큰 필드 (131072)
매우 큰 필드가있는 CSV 파일로 읽은 스크립트가 있습니다.
# 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 파일에서 다음 오류가 발생합니다.
_csv.Error: field larger than field limit (131072)
csv 파일은 매우 큰 필드를 포함 할 수 있으므로 다음을 늘리십시오 field_size_limit
.
import sys
import csv
csv.field_size_limit(sys.maxsize)
sys.maxsize
Python 2.x 및 3.x에서 작동합니다. sys.maxint
파이썬 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이있는 모든 시스템에서 작동해야 함).
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 파일에 작은 따옴표 나 큰 따옴표가 포함되어 있기 때문일 수 있습니다. CSV 파일이 탭으로 구분 된 경우 다음과 같이 열어보십시오.
c = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE)
아래는 전류 제한을 확인하는 것입니다
csv.field_size_limit()
밖으로 [20] : 131072
아래는 한도를 높이는 것입니다. 코드에 추가
csv.field_size_limit(100000000)
한도를 다시 확인하십시오
csv.field_size_limit()
밖으로 [22] : 100000000
이제 "_csv. 오류 : 필드가 필드 제한 (131072)보다 큽니다"라는 오류가 표시되지 않습니다.
csv 필드 크기는 [Python 3.Docs] : csv 를 통해 제어됩니다 . field_size_limit ( [new_limit] ) :
파서가 허용하는 현재 최대 필드 크기를 반환합니다. 경우 new_limit이 주어집니다,이 새로운 제한됩니다.
기본적으로 128k 또는 0x20000 ( 131072 ) 으로 설정되며 적절한 .csv에 충분합니다 .
>>> import csv >>> csv.field_size_limit() 131072
그러나이 크기보다 적어도 하나의 필드가 더 긴 .csv 파일 ( 올바른 인용 부호 및 구분 기호 포함 )을 처리하면 오류가 나타납니다.
오류를 없애려면 크기 제한을 늘려야합니다 (걱정을 피하기 위해 가능한 최대 값이 시도됩니다).
장면 뒤에 ( [GitHub] : python / cpython-(마스터) cpython / Modules / _csv.c 구현 세부 사항 확인)이 값을 보유하는 변수는 C long ( [Wikipedia] : C data types )이며 크기는 CPU 아키텍처 및 OS ( I L P ) 에 따라 다릅니다 . 고전적인 차이점 : 64 비트 OS 의 경우 긴 유형 크기 (비트)는 다음과 같습니다.
- 닉스 : 64
- 승 : 32
설정하려고 할 때 새로운 값이 긴 경계 에 있는지 확인하기 때문에 어떤 경우에는 또 다른 예외가 발생합니다 (이 경우는 Win에서 일반적입니다 ).
>>> import sys >>> csv.field_size_limit(sys.maxsize) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: Python int too large to convert to C long
To avoid running into this problem, set the (maximum possible) limit (LONG_MAX) using an artifice (thanks to [Python 3.Docs]: ctypes - A foreign function library for Python). It should work on Python 3 and Python 2, on any CPU / OS.
>>> import ctypes >>> csv.field_size_limit(int(ctypes.c_ulong(-1).value // 2)) 131072 >>> csv.field_size_limit() 2147483647
For more details on playing with C types boundaries from Python, check [SO]: Maximum and minimum value of C types integers from Python (@CristiFati's answer).
Sometimes, a row contain double quote column. When csv reader try read this row, not understood end of column and fire this raise. Solution is below:
reader = csv.reader(cf, quoting=csv.QUOTE_MINIMAL)
I just had this happen to me on a 'plain' CSV file. Some people might call it an invalid formatted file. No escape characters, no double quotes and delimiter was a semicolon.
A sample line from this file would look like this:
First cell; Second " Cell with one double quote and leading space;'Partially quoted' cell;Last cell
the single quote in the second cell would throw the parser off its rails. What worked was:
csv.reader(inputfile, delimiter=';', doublequote='False', quotechar='', quoting=csv.QUOTE_NONE)
Find the cqlshrc file usually placed in .cassandra directory.
In that file append,
[csv]
field_size_limit = 1000000000
참고URL : https://stackoverflow.com/questions/15063936/csv-error-field-larger-than-field-limit-131072
'Programing' 카테고리의 다른 글
파이썬에서 문자열 날짜를 타임 스탬프로 변환 (0) | 2020.05.14 |
---|---|
Mac OS X에서 gradle 설치 / 업그레이드 (0) | 2020.05.14 |
Angular를 사용하여 데이터 속성을 작성하는 방법 (0) | 2020.05.14 |
Linux에서 포트가 열려 있는지 효율적으로 테스트합니까? (0) | 2020.05.14 |
Sublime Text 2의 각 선택 항목에 선택 항목 당 한 번씩 숫자를 추가하십시오. (0) | 2020.05.14 |