Created
August 22, 2018 09:15
-
-
Save dasdachs/f72f7e38ebcabf4260544e85edcf9cc4 to your computer and use it in GitHub Desktop.
Poor mans csv parser
This file contains hidden or 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 poor_mans_csv_parser(text, delimiter=',', quote='"'): | |
assert isinstance(text, list), "At the momment we can not process strings." | |
parsed_text = [] | |
for line in text: | |
line = list(line) | |
parsed_line = [] | |
current = '' | |
quoted = False | |
while line: | |
current_char = line[0] | |
if current_char == '\n': | |
break | |
elif current_char == quote and not quoted: | |
quoted = True | |
line.pop(0) | |
elif current_char == quote and quoted: | |
line.pop(0) | |
quoted = False | |
elif current_char == delimiter and quoted == False: | |
parsed_line.append(current) | |
line.pop(0) | |
current = '' | |
else: | |
current += line.pop(0) | |
parsed_text.append(parsed_line) | |
return parsed_text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment