Skip to content

Instantly share code, notes, and snippets.

@scottanderson42
Created August 21, 2015 16:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save scottanderson42/8d8f2ee9869eab0ae221 to your computer and use it in GitHub Desktop.
import csv
# import sys
# Note: use sys.stdin and sys.stdout if you want to write a command line pipe
with open('data.txt', 'rb') as input_file:
with open('out.csv', 'wb') as csv_output_file:
currently_in_row = False
csv_writer = csv.writer(csv_output_file, dialect=csv.excel)
current_row = []
for line in input_file:
line = line.strip()
if not line:
if currently_in_row:
currently_in_row = False
csv_writer.writerow(current_row)
current_row = []
continue
if currently_in_row:
current_row.append(line)
elif line.startswith('='):
# Skip the next 2 lines
next(input_file)
next(input_file)
currently_in_row = True
else:
print 'WTF, mate?', '|', line, '|'
if currently_in_row:
# Take care of the last record after the file finishes.
csv_writer.writerow(current_row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment