Created
February 11, 2019 08:09
-
-
Save lucasnad27/7256d34cc02990bf7cd6bcc1cba2cb39 to your computer and use it in GitHub Desktop.
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
import csv | |
from pathlib import Path | |
def get_headers(): | |
# total hack | |
with open('File1.txt') as f: | |
headers = [] | |
for line in f: | |
if ':' not in line: | |
break | |
print(line) | |
column, _ = line.split(':') | |
headers.append(column.strip(' ')) | |
return headers | |
def process_file(file_name, w): | |
rows = [] | |
with open(file_name) as f: | |
current_row = {} | |
for line in f: | |
if ':' not in line: | |
rows.append(current_row) | |
current_row = {} | |
continue | |
column, value = line.split(':') | |
current_row[column.strip(' ')] = value.strip(' ').strip('\n') | |
w.writerows(rows) | |
def main(): | |
files = list(Path('.').glob('*.txt')) | |
with open('results.csv', 'w') as w: | |
writer = csv.DictWriter(w, get_headers()) | |
writer.writeheader() | |
for f in files: | |
print(f'processing {f}') | |
process_file(f, writer) | |
print('all done!') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment