Created
January 17, 2018 10:56
-
-
Save brunetton/146614dfd10f6fbeeaeefa3bc2e81ac8 to your computer and use it in GitHub Desktop.
Custom csv DictReader, as an iterator, that trunck "too long" lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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