I am trying to setup a website in django which allows the user to send queries to a database containing information about their representatives in the European Parliament. I have the data in a comma seperated .txt file with the following format:
Parliament, Name, Country, Party_Group, National_Party, Position
7, Marta Andreasen, United Kingdom, Europe of freedom and democracy Group, United Kingdom Independence Party, Member
etc....
I want to populate a SQLite3 database with this data, but so far all the tutorials I have found only show how to do this by hand. Since I have 736 observations in the file I dont really want to do this.
I suspect this is a simple matter, but I would be very grateful if someone could show me how to do this.
Thomas
So assuming your
models.py
looks something like this:You can then run
python manage.py shell
and execute the following:And you're done.
Addendum (edit)
Per Thomas's request, here's an explanation of what
**dict(zip(fields,row))
does:So initially,
fields
contains a list of field names that we defined, androw
contains a list of values that represents the current row in the CSV file.What
zip()
does is it combines two lists into one list of pairs of items from both lists (like a zipper); i.e.zip(['a','b,'c'], ['A','B','C'])
will return[('a','A'), ('b','B'), ('c','C')]
. So in our case:The
dict()
function simply converts the list of pairs into a dictionary.The
**
is a way of converting a dictionary into a keyword argument list for a function. Sofunction(**{'key': 'value'})
is the equivalent offunction(key='value')
. So in out example, callingcreate(**dict(zip(field, row)))
is the equivalent of:Hope this clears things up.
You asked what the create(**dict(zip(fields, row))) line did.
I don't know how to reply directly to your comment, so I'll try to answer it here.
zip takes multiple lists as args and returns a list of their correspond elements as tuples.
zip(list1, list2) => [(list1[0], list2[0]), (list1[1], list2[1]), .... ]
dict takes a list of 2-element tuples and returns a dictionary mapping each tuple's first element (key) to its second element (value).
create is a function that takes keyword arguments. You can use **some_dictionary to pass that dictionary into a function as keyword arguments.
create(**{'name':'john', 'age':5}) => create(name='john', age=5)
Something like the following should work: (not tested)
As SiggyF says and only slightly differently than Joschua:
Create a text file with your schema, e.g.:
Create table:
Use csv module to read file with data to be inserted:
Insert data:
Verify that all went as expected:
Edit:
And since you've decoded (to unicode) on input, you need to be sure to encode on output.
For example:
If you want to do it with a simple method using sqlite3, you can do it using these 3 steps:
However do keep the following points in mind:
.txt
file should be in the same directory as yourdb.sqlite3
,otherwise use an absolute path
"/path/myfile.txt"
when importingYou can use the
.tables
command to verify your table nameYou could read the data using the csv module. Then you can create an insert sql statement and use the method executemany:
or use add_all if you use sqlalchemy.