Skip to content

Instantly share code, notes, and snippets.

@mgrandi
Last active August 29, 2015 14:00
Show Gist options
  • Save mgrandi/11025212 to your computer and use it in GitHub Desktop.
Save mgrandi/11025212 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# script to convert csv from the Mileage Log wp7 app's email export format to json
# so the app can load it from OneDrive/SkyDrive
#
# written by Mark Grandi - Apr 16, 2014
#
import csv, json, argparse, sys, io, uuid, datetime
def main(args):
'''reads csv from mileage log wp7 and exports it to the json format it expects
@param args - the namespace object we get from argparse.parse_args()
'''
# since this isnt just a raw csv file we need to read 4 lines from it
createdLine = transDate(args.infile.readline().split(":", maxsplit=1)[1].strip())
lModifiedLine = transDate(args.infile.readline().split(":", maxsplit=1)[1].strip())
eCountLine = args.infile.readline().split(":", maxsplit=1)[1].strip()
descLine = args.infile.readline().split(":", maxsplit=1)[1].strip()
csvFile = io.StringIO(args.infile.read())
reader = csv.DictReader(csvFile, skipinitialspace=True) # make it so we don't have spaces at the beginning of key names
finalResultDict = dict()
# goes inside final result
metadataDict = dict()
metadataDict["Created"] = createdLine
metadataDict["Description"] = descLine
metadataDict["EntryCount"] = eCountLine
metadataDict["LastModified"] = lModifiedLine
theUuid = uuid.uuid4()
metadataDict["Id"] = str(theUuid)
metadataDict["Filename"] = "{}.journal".format(theUuid.hex)
# goes inside final result
entryList = list()
# go through csv part of file
for dictRow in reader:
# the dict that the csv reader returns is good for 5/6 fields
# however we need a 'Id' field which is just a uuid
dictRow["Id"] = str(uuid.uuid4())
dictRow["Date"] = transDate(dictRow["Date"]) # transform date into isoformat
dictRow["WasTankFilled"] = True if dictRow["WasTankFilled"] == "True" else False # turn string into boolean
entryList.append(dictRow)
# return final result
finalResultDict["entries"] = entryList
finalResultDict["metadata"] = metadataDict
args.outfile.write(json.dumps(finalResultDict, indent=4))
def transDate(dateStr):
dtObj = datetime.datetime.strptime(dateStr, "%m/%d/%Y %I:%M:%S %p")
return dtObj.isoformat() + "-07:00"
if __name__ == "__main__":
# if we are being run as a real program
parser = argparse.ArgumentParser(description="reads csv from mileage log wp7 and exports it to the json format it expects",
epilog="Copyright Apr 16, 2014 Mark Grandi")
parser.add_argument('infile', type=argparse.FileType('r'), help="the csv input file")
parser.add_argument('outfile', nargs="?", type=argparse.FileType('w'), default=sys.stdout,
help="the json output file, default is stdout")
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment