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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:19:58+00:00 2026-06-09T21:19:58+00:00

I have a text file. Each line of that file has either 6 fields

  • 0

I have a text file. Each line of that file has either 6 fields or 7 fields.

  • If there are 7 fields in a line, i am making first 6 fields as a key and 7th field as the value for that key.

  • If there are 6 fields in a line, i am making first 5 fields as a key and 6th field as the value for that key.

I don’t know if posting the entire code is necessary or not, but for a clear picture, i am posting the entire code.

My code is pasted below:

ReqResRS = {}
with contextlib.nested(open(sys.argv[1],'r'), open(sys.argv[2], 'w')) as (inpf, outf):
    lines = [l.split() for l in inpf if l.strip()]
    for l in lines:
            if(l[6]):
                    myKey = (l[0],l[1],l[2],l[3],l[4],l[5])
                    myValue = l[6]
                    if(myKey in ReqResRS):
                            diff = float(l[6])-float(ReqResRS[myKey]);
                            if(float(diff) < 0.000008):
                                    ReqResRS[myKey] = myValue
                            else:
                                    ReqResRS[myKey] = myValue
                                    outf.write(l[0] + "\t" + l[1] + "\t" + l[2] + "\t" + l[3] + "\t" + l[4] + "\t" + l[5] + "\t" + l[6] + "\n")
                    else:
                            ReqResRS[myKey] = myValue
                            outf.write(l[0] + "\t" + l[1] + "\t" + l[2] + "\t" + l[3] + "\t" + l[4] + "\t" + l[5] + "\t" + l[6] + "\n")
            else:
                    myKey = (l[0],l[1],l[2],l[3],l[4])
                    myValue = l[5]
                    if(myKey in ReqResRS):
                            diff = float(l[5])-float(ReqResRS[myKey]);
                            if(float(diff) < 0.000008):
                                    ReqResRS[myKey] = myValue
                            else:
                                    ReqResRS[myKey] = myValue
                                    outf.write(l[0] + "\t" + l[1] + "\t" + l[2] + "\t" + l[3] + "\t" + l[4] + "\t" + l[5] + "\n")
                    else:
                            ReqResRS[myKey] = myValue
                            outf.write(l[0] + "\t" + l[1] + "\t" + l[2] + "\t" + l[3] + "\t" + l[4] + "\t" + l[5] + "\n")
  • 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-09T21:20:00+00:00Added an answer on June 9, 2026 at 9:20 pm
    if(l[6]):
    

    fails if your array is to short.
    Accessing a value that is not part of the array will allways result in an exception. Therefore you should check beforehand whether the array is long enough, like this:

    if len(l) == 7:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that has strings on each line. I want to
I have a text file with lots of links-each line has a link (i.e
I have a text file where each line may end with some fixed TAG
If I have a text file with a separate command on each line how
I have a text file like this: A B C And each element has
I have a text file with several different sections. Each section has a header
I have two 3GB text files, each file has around 80 million lines. And
I have a form that allows the user to either upload a text file
I have a text file, one thing on each line. I also have a
I have a huge text file, each line seems like this: Some sort of

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.