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 7179365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:07:31+00:00 2026-05-28T17:07:31+00:00

I am using the csv module for Python. I have had a good look

  • 0

I am using the csv module for Python. I have had a good look at the CSV File Reading and Writing guide. I want to write a loop that runs through each row in the CSV file and assigns each row do a different variable. Does anyone have any ideas on this?

I am aware that there is a .next() and .line_num, I didn’t think that these would be suitable in this case although I might be wrong.

Currently I have the following code, which print out the whole CSV file:

print_csv = csv.reader(open(csv_name, 'rb'), delimiter=' ', quotechar='|')
for row in print_csv:
    print ', '.join(row)

[EDIT]

I am now aware, from this question thread, that the best way to do this will depend on what the first line is going to be used for.

What I want to do with the first line of the CSV file is to check whether it is in the correct format. This would involve:

  • checking to see whether it has the expected number of columns

  • checking to see whether the column headers have the correct name

  • checking to see whether to columns are in the correct order.

  • 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-28T17:07:33+00:00Added an answer on May 28, 2026 at 5:07 pm

    1.- Fast Answer

    Instead of setting different independent variables you could do:

    mydict = {}
    for idx, item in enumerate(reader):
        mydict['var%i' %idx] = item
    

    then you call your var like:

    mydict['var0']
    

    Or still shorter in py3k:

    mydict = {'var%i' %idx : item for idx, item in enumerate(reader)}
    

    But this doesnt have much sense applied this way

    As a commenter said this is not different than doing directly:

    mylist = list(reader)
    

    and then

    mylist[0] # instead of 'var0'
    

    and this option is much better.
    The dictionary strategy is best suited when you extract the dictionary key from the very same reader line. For example, if it were at pos pos 0,:

    mydict = {item[0] : item for item in reader}
    

    2.- The Proper Answer

    But if what you want is simply to check the format of the first line (maybe to calculate the space you need for printing), the method could be:

    line = reader.next()
    like_this = check_how_is_my(line)
    if like_this == 'something_long':
        spaces = 23
    else:
        spaces = 0
    
    while True:
        try:
            print_with_spaces(line, spaces)
            reader.next()
        except StopIteration:
            break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Python's csv module to do some reading and writing of csv files.
I am trying to write to a .tsv file using python's CSV module, this
I'm trying to parse a CSV file using Python's csv module (specifically, the DictReader
I have a CSV file with 5 columns. Using Python, how can I delete
I have a UTF-16 CSV file which I have to read. Python csv module
How do I import a CSV file using OpenOffice.org APIs? I want to do
I am using the following method for reading Csv file content: /// <summary> ///
I have a small script we're using to read in a CSV file containing
I am using python's csv module to extract data from a csv that is
I am trying to create a csv file using python that is truly Excel-compatible

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.