2
我想使用csv.DictReader讀取TSV文件,但是,讀者報告錯誤的字段數。當我看着裏面,我發現這不是分裂的分隔符正常(我證實str.split(delimiter)
和awk -F 'delim'
python3的csv.DictReader沒有像預期的那樣通過分隔符分割記錄
from pprint import pprint
import csv
import sys, os
keys = ['id', 'src', 'src_len', 'sys1_o', 'sys1_b', 'sys1_l',
'sys2_o', 'sys2_b', 'sys2_l', 'sys3_o', 'sys3_b', 'sys3_l', 'x']
# get this data from https://gist.github.com/thammegowda/95613b203a442fbe72fc5b51af491367
my_data = """segment-22 Kture . 27 Thutaalchisu. - 33 Koture. - 27 Th jump. - 33 3
segment-23 ‘Yunker . 7 "said. - 8 ‘Yunker. - 7 "said. - 8 1"""
tmp_file = "tmp.tsv"
with open(tmp_file, 'w', encoding='utf-8') as f:
f.write(my_data)
def read_recs_csv(path):
with open(path, 'rt', encoding='utf-8') as f:
rdr = csv.DictReader(f, fieldnames=keys, delimiter='\t')
for rec in rdr:
yield(dict(rec))
def read_recs_raw(path):
with open(path, 'rt', encoding='utf-8') as f:
for line in f:
rec = dict(zip(keys, line.strip().split('\t')))
yield(rec)
print("Reading through CSV DictReader ")
pprint(list(read_recs_csv(tmp_file)))
print("Reading directly")
pprint(list(read_recs_raw(tmp_file)))
# Debug
print(sys.version_info)
print(os.environ['LANG'])
輸出:
Reading through CSV DictReader
[{'id': 'segment-22',
'src': 'Kture .',
'src_len': '27',
'sys1_b': '-',
'sys1_l': '33',
'sys1_o': 'Thutaalchisu.',
'sys2_b': '-',
'sys2_l': '27',
'sys2_o': 'Koture.',
'sys3_b': '-',
'sys3_l': '33',
'sys3_o': 'Th jump.',
'x': '3'},
{'id': 'segment-23',
'src': '‘Yunker .',
'src_len': '7',
'sys1_b': '-',
'sys1_l': '8',
'sys1_o': 'said.\t-\t8\t‘Yunker.\t-\t7\tsaid.',
'sys2_b': None,
'sys2_l': None,
'sys2_o': '1',
'sys3_b': None,
'sys3_l': None,
'sys3_o': None,
'x': None}]
Reading directly
[{'id': 'segment-22',
'src': 'Kture .',
'src_len': '27',
'sys1_b': '-',
'sys1_l': '33',
'sys1_o': 'Thutaalchisu.',
'sys2_b': '-',
'sys2_l': '27',
'sys2_o': 'Koture.',
'sys3_b': '-',
'sys3_l': '33',
'sys3_o': 'Th jump.',
'x': '3'},
{'id': 'segment-23',
'src': '‘Yunker .',
'src_len': '7',
'sys1_b': '-',
'sys1_l': '8',
'sys1_o': '"said.',
'sys2_b': '-',
'sys2_l': '7',
'sys2_o': '‘Yunker.',
'sys3_b': '-',
'sys3_l': '8',
'sys3_o': '"said.',
'x': '1'}]
sys.version_info(major=3, minor=6, micro=1, releaselevel='final', serial=0)
en_US.UTF-8
注:樣本數據張貼在a gist。如果標籤被空格替換,請下載它
你能提供一個你的csv內容的例子嗎? – asongtoruin
注意只提供了12個密鑰,因爲有13列,讀者添加一個額外的'None'鍵來分組其他值。那是故意的嗎? –
@ Jean-FrançoisFabre是的,我只是想忽略其他鍵 –