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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:00:13+00:00 2026-06-06T15:00:13+00:00

I am a beginner of Python. I am trying now figuring out why the

  • 0

I am a beginner of Python. I am trying now figuring out why the second ‘for’ loop doesn’t work in the following script. I mean that I could only get the result of the first ‘for’ loop, but nothing from the second one. I copied and pasted my script and the data csv in the below.

It will be helpful if you tell me why it goes in this way and how to make the second ‘for’ loop work as well.

My SCRIPT:

import csv

file = "data.csv"

fh = open(file, 'rb')
read = csv.DictReader(fh)

for e in read:
    print(e['a'])

for e in read:
    print(e['b'])

“data.csv”:

a,b,c
tree,bough,trunk
animal,leg,trunk
fish,fin,body
  • 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-06T15:00:14+00:00Added an answer on June 6, 2026 at 3:00 pm

    The csv reader is an iterator over the file. Once you go through it once, you read to the end of the file, so there is no more to read. If you need to go through it again, you can seek to the beginning of the file:

    fh.seek(0)
    

    This will reset the file to the beginning so you can read it again. Depending on the code, it may also be necessary to skip the field name header:

    next(fh)
    

    This is necessary for your code, since the DictReader consumed that line the first time around to determine the field names, and it’s not going to do that again. It may not be necessary for other uses of csv.

    If the file isn’t too big and you need to do several things with the data, you could also just read the whole thing into a list:

    data = list(read)
    

    Then you can do what you want with data.

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

Sidebar

Related Questions

I'm trying to get simple code working, unfortunately I'm a python beginner. My script
I was trying to figure out how to work with SQLite database using python,
Right I am a complete beginner here, trying to work through various python tutorials
I am learning Python, following a book (Python Programming for the Absolute Beginner, 3rd
I am a beginner with Python and trying few programs. I have something like
I'm a beginner in python and I'm trying to use a octal number in
Beginner in python, but been programming for about 5 years now. I suspect I
I am a beginner to python and any coding at all for that matter,
I'm a beginner programmer and my first language is Python 2.7, I'm trying to
First, let me say that I'm a complete beginner at Python. I've never learned

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.