Reputation: 11947
I am trying to read a TSV file using csv.DictReader, however, the reader is reporting wrong number of fields. When I looked inside, I found that it is not splitting delimiter properly (I confirmed with str.split(delimiter)
and awk -F 'delim'
.
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'])
Output:
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
Note: sample data is posted in a gist. Please download it if tabs are replaced by spaces here.
Upvotes: 2
Views: 1611
Reputation: 140196
The difference between raw and csv parsing (and that's why I insisted to get the input data) is that csv module handles quoting by default.
There are quotes in your data, and csv
consider quote-protected fields as a single field. awk
and str.split
don't care.
Just tell csv
module not to consider quoting:
rdr = csv.DictReader(f, fieldnames=keys, delimiter='\t', quoting=csv.QUOTE_NONE)
doing this, I get all fields filled.
Upvotes: 1