Skip to content

Instantly share code, notes, and snippets.

@dasdachs
Created August 22, 2018 09:15
Show Gist options
  • Save dasdachs/f72f7e38ebcabf4260544e85edcf9cc4 to your computer and use it in GitHub Desktop.
Save dasdachs/f72f7e38ebcabf4260544e85edcf9cc4 to your computer and use it in GitHub Desktop.
Poor mans csv parser
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