/row_per_line.py Secret
Created
August 21, 2015 16:55
Star
You must be signed in to star a gist
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
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