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

  • Home
  • SEARCH
  • 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 6021627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:44:26+00:00 2026-05-23T03:44:26+00:00

I want to read a CSV file’s columns directly into variables. The result should

  • 0

I want to read a CSV file’s columns directly into variables. The result should be something like you would get with the following shell line:
while IFS=, read ColumnName1 ColumnName2 ColumnName3
do stuff

So far the answer seems to be with csv.DictReader, but I have not been able to make it work. I do not have a header row, so column names would have to be created manually. (with a dictionary I think in the form mydictionary={ ‘ColumnName1’:0, ‘ColumnName2’:1, ‘ColumnName3’:3 } )

Also, can the columns be referenced as simple variable names or must you use a list[index] style reference. A code sample just printing the columns by name would be nice. Thanks for the help.

  • 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-23T03:44:27+00:00Added an answer on May 23, 2026 at 3:44 am

    The built-in CSV Module is quite useful when working with csv files.


    Oh, nevermind, you must be using it already, if you are looking at DictReader.

    The usual way I deal with files that have no header would be to read the first line, parse it for the number of commas (and hence the number of columns) then set up my dictionary/list to contain the values from the csv file (using number of columns and giving each column a name in my my code.) I can provide an example if necessary, it’s pretty straightforward.


    I think I better understand your question, is this more what you are looking for?:

    mydictionary={ 'ColumnName1':[dataRow1Col1, dataRow2Col1, dataRow3Col1], 
                   'ColumnName2':[dataRow1Col2, dataRow2Col2, dataRow3Col2], 
                   'ColumnName3':[dataRow1Col3, dataRow2Col3, dataRow3Col3] }
    

    In which case, something like this may work:

    import csv
    Col1 = "ColumnName1"
    Col2 = "ColumnName2"
    Col3 = "ColumnName3"
    mydictionary={Col1:[], Col2:[], Col3:[]}
    csvFile = csv.reader(open("myfile.csv", "rb"))
    for row in csvFile:
      mydictionary[Col1].append(row[0])
      mydictionary[Col2].append(row[1])
      mydictionary[Col3].append(row[2])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read a csv file into an access database , here is
I have a CSV file that I want to read into a List. Here's
I want to read .csv file in PHP and put its contents into the
I want to read line n1->n2 from file foo.c into the current buffer. I
I want to read in a csv file, filter it based on the values
I want to import the contents of a csv file into R, the csv
I have a CSV file that I want to read with Ruby and create
I want to read data from a CSV file using microsoft text driver. Can
I want to read a .csv file in python. I don't know if the
I have following sample CSV file rc,u,s,ui,gh m,1,8,0,12 n,3,0,0,7 d,1,1,8,0 I want to read

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.