Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import re
- fieldnames = ['NAME', 'DESCR', 'PID', 'VID', 'SN']
- with open('test.csv', 'w') as output:
- writer = csv.DictWriter(output, fieldnames=fieldnames)
- writer.writeheader()
- with open('input.txt', 'r') as input:
- lines = input.readlines()
- for i in range(1, len(lines), 3):
- line = lines[i].strip() + "," + lines[i + 1].strip()
- sections = re.split("([A-Z])\w+:", line)
- curCol = {}
- for j in range(1, len(sections), 2):
- key = fieldnames[j / 2]
- value = sections[j + 1].strip().strip(",").strip("\"")
- curCol[key] = value
- writer.writerow(curCol)
Advertisement
Add Comment
Please, Sign In to add comment