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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:10:59+00:00 2026-05-23T09:10:59+00:00

Can someone help me with this nested loop? it has the same problem as

  • 0

Can someone help me with this nested loop? it has the same problem as Loops not working – Strings (Python) but now it is in a csv class that doesn’t have a csv.readline() function.

import csv
import sys, re
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')

reader = csv.reader(open("reference.txt"), delimiter = "\t")
reader2 = csv.reader(open("current.txt"), delimiter = "\t")

for line in reader:
    for line2 in reader2:
        if line[0] == line2[1]:
            print line2[0] + '\t' + line[0]
            print line[1]
        else:
            print line[0]
            print line[1]

The purpose of this code is to check the lines in a reference text (i.e. reader2) that coincide with the current textfile (i.e. reader). And then print the serial number that is in the reference.txt

reference.txt looks like this (the space between the serial no. and sentence is a tab):

S00001LP    this is a nested problem
S00002LP    that cannot be solved
S00003LP    and it's pissing me off
S00004LP    badly

current.txt looks like this(the space between the 1st and 2nd sentence is a ):

this is a nested problem    wakaraa pii ney bay tam
and i really can't solve it    shuu ipp faa luiip
so i come to seek help from stackoverflow    lakjsd sdiiije
seriously it is crazy because such    foo bar bar foo
problems don't happen in other languages    whaloemver ahjd
and it's pissing me off    gaga ooo mama
badly    wahahahah

the required output will look something like this:

S00001LP    this is a nested problem    wakaraa pii ney bay tam
and i really can't solve it    shuu ipp faa luiip
so i come to seek help from stackoverflow    lakjsd sdiiije
seriously it is crazy because such    foo bar bar foo
problems don't happen in other languages    whaloemver ahjd
S00003LP    and it's pissing me off    gaga ooo mama
S00004LP    badly    wahahahah
  • 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-23T09:10:59+00:00Added an answer on May 23, 2026 at 9:10 am

    You can only read from a stream once. Your inner loop is consuming the second file too quickly, and other iterations of your outer loop don’t have a chance to read the second file again.

    Try changing this:

    reader = csv.reader(open("reference.txt"), delimiter = "\t")
    reader2 = csv.reader(open("current.txt"), delimiter = "\t")
    

    to this:

    reader = list(csv.reader(open("reference.txt"), delimiter = "\t"))
    reader2 = list(csv.reader(open("current.txt"), delimiter = "\t"))
    

    The list() will read the file in its entirety, creating an in-memory list from it, which you can then iterate as many times as your like.

    A better solution would be to store your reference data in a dictionary so that you don’t have to loop over it for every line in your data.

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

Sidebar

Related Questions

I'm hoping someone has run into this problem before and can help me out.
Can someone please help me out with a JavaScript/jQuery solution for this arithmetic problem:
can someone please help me turn this nested structure into a single LINQ statement?
can someone help with this? I need the following function to do this... $x
Can someone help me with this: This is a program to find all the
Can someone help me on this. I'm made an image uploader and i want
Can someone help me identify what the purpose of this unidentified syntax is. It
Can someone help me create an index/count button for a UITableView, like this one?
can someone please help me. why does this return an error: Dim stuff As
I hope someone can help me with this, as I'm unable to find the

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.