Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 918269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:20:48+00:00 2026-05-15T18:20:48+00:00

I am trying to setup a website in django which allows the user to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:20:49+00:00Added an answer on May 15, 2026 at 6:20 pm

    So assuming your models.py looks something like this:

    class Representative(models.Model):
        parliament = models.CharField(max_length=128)
        name = models.CharField(max_length=128)
        country = models.CharField(max_length=128)
        party_group = models.CharField(max_length=128)
        national_party = models.CharField(max_length=128)
        position = models.CharField(max_length=128)
    

    You can then run python manage.py shell and execute the following:

    import csv
    from your_app.models import Representative
    # If you're using different field names, change this list accordingly.
    # The order must also match the column order in the CSV file.
    fields = ['parliament', 'name', 'country', 'party_group', 'national_party', 'position']
    for row in csv.reader(open('your_file.csv')):
        Representative.objects.create(**dict(zip(fields, row)))
    

    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, and row contains a list of values that represents the current row in the CSV file.

    fields = ['parliament', 'name', 'country', ...]
    row = ['7', 'Marta Andreasen', 'United Kingdom', ...]
    

    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:

    >>> zip(fields, row)
    [('parliament', '7'), ('name', 'Marta Andreasen'), ('country', 'United Kingdom'), ...]
    

    The dict() function simply converts the list of pairs into a dictionary.

    >>> dict(zip(fields, row))
    {'parliament': '7', 'name': 'Marta Andreasen', 'country': 'United Kingdom', ...}
    

    The ** is a way of converting a dictionary into a keyword argument list for a function. So function(**{'key': 'value'}) is the equivalent of function(key='value'). So in out example, calling create(**dict(zip(field, row))) is the equivalent of:

    create(parliament='7', name='Marta Andreasen', country='United Kingdom', ...)
    

    Hope this clears things up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.