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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:26:39+00:00 2026-06-11T13:26:39+00:00

I am an absolute programming novice trying to work with some csv files. Though

  • 0

I am an absolute programming novice trying to work with some csv files. Though what I am trying to do overall is more complex, I am currently stuck on this problem:

The csv files I have contain a fixed number of ‘columns’ and a variable number of rows. What I want to do is open each csv file in a directory, while in memory store the files values to a 2d list, and then pull one ‘column’ of data from that list. By doing this in a loop, I could append a list with one column of data from each csv file.

When I do this for a single file, it works:

csvFile = 'testdata.csv'
currentFile = csv.reader(open(csvFile), delimiter=';')
errorValues = []

    for data in currentFile:

        rows = [r for r in currentFile] #Store current csv file into a 2d list           
        errorColumn = [row[34] for row in rows] #Get position 34 of each row in 2D list
        errorColumn = filter(None, errorColumn) #Filter out empty strings
        errorValues.append(errorColumn) #Append one 'column' of data to overall list

When I try to loop it for all files in my directory, I get a ‘list index out of range’ error:

dirListing = os.listdir(os.getcwd())    
errorValues = []

for dataFile in dirListing:
    currentFile = csv.reader(open(dataFile), delimiter=';')        

    for data in currentFile:

        rows = [r for r in currentFile] #Store current csv file into a 2d list           
        errorColumn = [row[34] for row in rows] #Get position 34 of each row in 2D list
        errorColumn = filter(None, errorColumn) #Filter out empty strings
        errorValues.append(errorColumn) #Append one 'column' of data to overall list

    errorColumn = [] #Clear out errorColumn for next iteration

The error occurs at ‘errorColumn = [row[34] for row in rows]’. I have tried all sorts of ways to do this, always failing to an index out of range error. The fault is not with my csv files as I have used the working script to test them one by one. What could be the problem?

Many thanks for any 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-06-11T13:26:41+00:00Added an answer on June 11, 2026 at 1:26 pm

    The for loop goes through the lines of the CSV file. Each line is converted to the row of element by the reader. This way, the data in the loop is already the row. The next construct also iterates through the open file. This is wrong.

    There is a problem with your open(). The file must be opened in binary mode (in Python 2).

    Try the following (I did not put everything you wanted inside):

    dirListing = os.listdir(os.getcwd())    
    errorValues = []
    
    rows = []                  # empty array of rows initially
    
    for fname in dirListing:
        f = open(fname, 'rb')  # open in binary mode (see the doc)
        reader = csv.reader(f, delimiter=';')        
    
        errorColumn = []       # initialized for the file
    
        for row in reader:
            rows.append(row) #Store current csv file into a 2d list           
            if len(row) > 34:
                errorColumn.append(row[34]) #Get position 34 of each row in 2D list
    
        errorValues.append(errorColumn)
    
        f.close()              # you should always close your files
    

    Beware! The os.listdir() returns also the names of subdirectories. Try to add

    if os.path.isfile(fname):
        ...
    

    By the way, you should clearly describe what is your actual goal. There may be a better way to solve it. You may be mentally fixed to the solution that came first to your mind. Take advantage of this media to have more eyes and more headst to suggest the solution.

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

Sidebar

Related Questions

I don't know is this question more related to mathematics or programming and I'm
I'm programming a little game in SDL and the files are structured like this:
This may be a math question more than a programming question, but we'll see.
I'm an absolute beginner to programming and i'm just doing some exercises exercises for
This question is more logic than programming at the moment.. once I understand what
I am an absolute newbie to Perl as well as programming in general(less than
I am learning Python, following a book (Python Programming for the Absolute Beginner, 3rd
Absolute beginner question: I have a template file index.html that looks like this: ...
Disclaimer: absolute novice in Scala :( I have the following defined: def tryAndReport(body: Unit)
I'm almost an absolute beginner in Python, but I am asked to manage some

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.