Created
December 23, 2015 22:33
-
-
Save konklone/d095fb7224716c9212e6 to your computer and use it in GitHub Desktop.
converts ua-capabilities.txt TSV into a CSV file
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
#!/usr/bin/env python | |
import csv | |
import re | |
input_file = "ua-capabilities.txt" | |
# double-quote the quotes, save to an intermediary file. | |
# double-quoting is necessary to parse correctly, otherwise you eventually get: | |
# _csv.Error: field larger than field limit (131072) | |
# but it does mean cells need to be un-double-quoted before rewritten to CSV. | |
intermediary = "ua-intermediary.txt" | |
raw_input = open(input_file, 'r', newline='', encoding='latin1').read() | |
raw_input = re.sub("\"", "\"\"", raw_input) | |
with open(intermediary, "w") as quoted: | |
quoted.write(raw_input) | |
# transform to CSV | |
with open('ua-capabilities.csv', 'w') as csvout: | |
tsvin = csv.reader(open(intermediary), delimiter='\t') | |
csvout = csv.writer(csvout) | |
for row in tsvin: | |
# un-double-quote cells (eye roll) | |
row[1] = re.sub("\"\"", "\"", row[1]) | |
csvout.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment