Skip to content

Instantly share code, notes, and snippets.

@sicking
Created January 18, 2017 09:01
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 sicking/fd38436a56f5363012d78fa8645cb8fb to your computer and use it in GitHub Desktop.
Save sicking/fd38436a56f5363012d78fa8645cb8fb to your computer and use it in GitHub Desktop.
#! /usr/bin/python
import sys, re
checkedVersion = False
ignoredHeaders = False
with open(sys.argv[1], 'rU') as csvfile:
for row in csvfile:
if not checkedVersion:
checkedVersion = True
attrs = re.split('\s*;\s*', row);
if attrs[1] != "Export format 1.1" or \
attrs[0] != "# Generated by Password Exporter":
sys.exit("Unknown format: " + row)
continue
if not ignoredHeaders:
ignoredHeaders = True
continue
fields = row[1:-1].split('","')[:3]
if not (fields[0].startswith('http://') or fields[0].startswith('https://')):
continue
fields = map(lambda str: re.sub("%(..)", lambda x: chr(int(x.group(1), 16)), str), fields)
fields[0:0] = [re.sub("^https?://(www\.|m\.|mobile\.)?", "", fields[0])]
print ','.join(map(lambda str: '"' + str.replace('"', '""') + '"', fields))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment