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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:46:58+00:00 2026-06-06T11:46:58+00:00

Hi I have two text files that each contain different information about certain structures.The

  • 0

Hi I have two text files that each contain different information about certain structures.The structures are identified in both files by an id number. What I would like to do is read in the first file and skip lines of data that do not meet a condition. Then have lines in the second file that do not have the same id number skipped and the others processed. I tried to do it using nested for loops, and I also tried it as two separate function, but neither attempt worked. I am now trying to get it done using one loop as below, but get this error

UnboundLocalError: local variable 'linel' referenced before assignment

Here is the code. I

F = 'file1.txt'
Fl = 'file2.txt'


 X = []               # Creats Data Lists
 M = []
 Id1 = []
 Id2 = []
 LC = 10
 N = 11

 fl = open(Fl)
 fl.readline()
 nlinesl = islice(fl,N)
 f = open(F)           #Opens file
 f.readline()          # Strips Header
 nlines = islice(f, N) #slices file to only read N lines


 for line in nlines and linel in nlinesl:             
    if line !='':
     linel = linel.strip()
     linel = linel.replace('\t','')
     columnsl = linel.split()
     lum = float(columnsl[1])
     if lum != LC:
      continue
     id1 = int(columnsl[0])
     Id1.append(id1)
     if line !='':
       line = line.strip()
       line = line.replace('\t','')
       columns = line.split()
       id2 = int(columns[1])
       Id2.append(id2)
       if Id !=  Id2:
        continue  
       x = columns[2]             # assigns variable to columns
       X.append(x)


  print(X)

here is an example of what I would like to happen
Two files

file1= 1 1 1 1     file2 =  1 1 1 1
       2 5 1 1              1 2 1 1
       2 3 4 4              1 1 1 1
Lc = 5
Xa = 1

So only the second line of file1 will survive, which means that only the second line in file2 will be processed because they have the same id. In my files the ids are

 id = columns[0] for file1 

and

 id = columns[1] for file2

Thanks in advance

  • 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-06T11:47:00+00:00Added an answer on June 6, 2026 at 11:47 am

    I don’t think this is legal syntax:

     for line in nlines and linel in nlinesl:           
    

    Try this instead:

     for line, line1 in zip(nlines, nlines1):
    

    Also, these are tricky/easy to get wrong variable names 🙂

    E.g.,

    a = range(20, 26)
    b = range(200, 226)
    
    #for i in a and j in b:  # causes 
    #   print i, j           # error
    
    for i, j in zip(a, b):
       print i, j
    

    generates output as expected

    20 200
    21 201
    22 202
    23 203
    24 204
    25 205
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two text files that contain columnar data of the variety position -
I have two large (~100 GB) text files that must be iterated through simultaneously.
I have two text files in two different languages and they are aligned line
I have two text files with various columns, each column is separated by a
I have two 3GB text files, each file has around 80 million lines. And
I have an asp page (Default.aspx) that displays the diff between two text files.
So I have two files, one that contains my text, and another which I
I have a large number of text files (1000+) each containing an article from
I have two text files. hash_only.txt and final_output.txt hash_only.txt looks like below. 193548 401125
I have two text files, I want to place a text in the middle

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.