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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:19:48+00:00 2026-05-19T09:19:48+00:00

I know sed or awk can tackle this kind of problem more elegantly perhaps.

  • 0

I know sed or awk can tackle this kind of problem more elegantly perhaps. But I went the python way, so the problem is that I would like to renumber the first column of my data file from 1 to #of lines in the file. Is that a good idea to read the file by readlines? For small files perhaps, but large files not I suppose. So here is what I came up as a first attempt, any comments are appreciated.

#!/usr/bin/env python

import sys

try:
    infilename = sys.argv[1]; outfilename = sys.argv[2];
except:
    print "Usage is <script> inFile outFile"

ifile = open(infilename,'r')
ofile = open(outfilename, 'w')

lines = ifile.readlines();

i=1
for line in lines: 
    list = line.split();
    list[0] = i
    i += 1 
    for val in list:
        ofile.write("%d " % int(val))
    ofile.write('\n')
    del list

ifile.close()
ofile.close()
  • 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-19T09:19:49+00:00Added an answer on May 19, 2026 at 9:19 am

    You can iterate over the file to keep only the current line in memory:

    #!/usr/bin/env python
    import sys
    
    try:
        # dont use ; !
        infilename = sys.argv[1]
        outfilename = sys.argv[2]
    except:
        print "Usage is <script> inFile outFile"
    
    
    # you could use `with` here if you have a Python 2.7
    ifile = open(infilename,'r')
    ofile = open(outfilename, 'w')
    
    # no need to count yourself, enumerate does that
    # plus when you iterate over a file you get lines too
    for i, line in enumerate(ifile, start=1):
        # dont shadow builtins like `list`
        parts = line.split()
        parts[0] = i
        # join is the inverse function to split
        new_line = ' '.join("%d" % int(val) for val in parts)
        ofile.write(new_line + '\n')
    
    ifile.close()
    ofile.close()
    

    @Umut Tabak: ("%d" % int(val) for val in parts) is a generator expression, they are kind of like lazy lists. It gives the same items as the list comprehension ["%d" % int(val) for val in parts] but without actually creating the list.

    Btw, the for block can be written even shorter, but it’s slightly different because it doesn’t enforce that all lines are ints anymore:

    for i, line in enumerate(ifile, start=1):
        parts = line.split()
        parts[0] = "%d" % i
        new_line = ' '.join(parts)
        ofile.write(new_line + '\n')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is silly but I can't overcome my curiosity. Is it possible
How can I call awk or sed inside a c program? I know I
I know that you can use this to remove blank lines sed /^$/d and
Know this might be rather basic, but I been trying to figure out how
I know that it's a subject that can raise a lot of debate, but
I know this is fairly subjective, but I'm diving into testing and learning about
I know this question has kind-a started religious wars in past and there might
I know this is largely an opinion, but I'm interested if you have one
i wont to cut my string with sed but i dont know how it
It needs to be graphical. No sed, awk, grep, perl, whatever. I know how

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.