Skip to content

Instantly share code, notes, and snippets.

@konklone
Created December 23, 2015 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konklone/d095fb7224716c9212e6 to your computer and use it in GitHub Desktop.
Save konklone/d095fb7224716c9212e6 to your computer and use it in GitHub Desktop.
converts ua-capabilities.txt TSV into a CSV file
#!/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