Skip to content

Instantly share code, notes, and snippets.

@Alir3z4
Created March 29, 2012 03:00
Show Gist options
  • Save Alir3z4/2232817 to your computer and use it in GitHub Desktop.
Save Alir3z4/2232817 to your computer and use it in GitHub Desktop.
make django fixtures in json format
__author__ = 'alireza'
import re
import datetime
f = open('/home/alireza/countries.sql')
matches = re.findall(r"'([A-Z]{0,2})','([A-Z,. \"\(\)\-]+)','([\w\\,. \"\(\)\-]+)','?([A-Z]{0,4})'?,'?([\w]{0,4})'?",
f.read())
print '['
for match in matches:
print ('\n\t{\n\t\t"model": "locality.country",\n\t\t'\
'"pk": "%s",\n\t\t'\
'"fields": {\n\t\t\t'\
'"name" : "%s",\n\t\t\t'\
'"printable_name" : "%s",\n\t\t\t'\
'"iso3" : "%s",\n\t\t\t'\
'"numcode" : "%s",\n\t\t\t'\
'"user" : 1,\n\t\t\t'\
'"created" : "%s"\n\t\t\t'\
'}\n\t'\
'},') % (match[0], match[1].replace('"', "'"), match[2].replace('"', "'"), match[3], match[4], str(datetime.date.today()))
print ']'
# on the new db schema, 'modified' field should be removed.
# also some modification shoulda be applied :|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment