Skip to content

Instantly share code, notes, and snippets.

@brunetton
Created January 17, 2018 10:56
Show Gist options
  • Save brunetton/146614dfd10f6fbeeaeefa3bc2e81ac8 to your computer and use it in GitHub Desktop.
Save brunetton/146614dfd10f6fbeeaeefa3bc2e81ac8 to your computer and use it in GitHub Desktop.
Custom csv DictReader, as an iterator, that trunck "too long" lines
def custom_dict_reader(csv_filename, delimiter="\t"):
with open(csv_filename) as csv_file:
header = list(map(str.strip, csv_file.readline().split(delimiter)))
line_num = 2
for line in csv_file.readlines():
splitted = list(map(str.strip, line.split(delimiter)))
if len(splitted) != len(header):
print("WARNING: the number of rows in line ({}) doesn't match the number of elements in header ({}) ! Row will be truncated".format(
len(splitted), len(header)
))
values_dict = OrderedDict(zip(header, splitted))
yield values_dict
line_num += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment