在用 Python 处理 csv 文件时遇到2个错误,记录下处理方法。
字段包含 NULL 值
csv 文件中字段包含 NULL 值会出错,解决方法是读取文件时把 NULL 值替换为空字符串。
1 2 3 4 5
| import csv
with open('test.csv', 'rt', encoding='utf-8') as f: fc = csv.DictReader((line.replace('\0', '') for line in f))
|
OverflowError and maxInt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import csv import sys
maxInt = sys.maxsize decrement = True
while decrement:
decrement = False try: csv.field_size_limit(maxInt) except OverflowError: maxInt = int(maxInt / 10) decrement = True
with open('test.csv', 'rt', encoding='utf-8') as f: fc = csv.DictReader(f)
|