Reading lines from a data file

Discussion in 'Python' started by shoemoodoshaloo, May 20, 2009.

  1. shoemoodoshaloo

    shoemoodoshaloo

    Joined:
    May 20, 2009
    Messages:
    1
    Likes Received:
    0
    Hey guys,

    I'm new to the forum and hope to learn a great deal and someday even contribute.

    I have the following basic assignment:

    Take lines from some data file, call it input, and write them to an output. What I want to do is have the user specify the range (for example lines 10-20) that will be picked from the input file to the output file. I am trying to use readlines() and I am able to get the program to pick a certain number of lines, but it always begins as line 1. For example, if I specify lines (30-200) it will recognize that it must extract 170 lines from the input file; however, it starts at line 1 and runs to line 170 instead of starting at line 30. Here is a snipet of the code:

    first = int(raw_input('Enter a starting value'))
    last = int(raw_input('Enter a final value'))

    def add_line_numbers(infile, outfile):
    f = open(infile,'r')
    o = open(outfile,'w')
    i = 1


    for i in range(first, last):
    o.write(str(i)+'\t'+f.readline())


    f.close()
    o.close()


    --- Parsing code follows


    The code originally worked fine until I began editing it. The original code takes an entire input file and transfers it to an entire output file. So only my edits are in question.

    The 'i' acts as a counter, and that part works fine. The counter will read 30-200 for example; however, the lines that are inputing are still 1-170 from the original data type.

    As I said, I am new to python and very amenable to suggestions so if you have a smarter way to tackled this problem, I am all ears.
     
    shoemoodoshaloo, May 20, 2009
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. srinukasam

    file lines reading

    srinukasam, Aug 5, 2005, in forum: VHDL
    Replies:
    0
    Views:
    615
    srinukasam
    Aug 5, 2005
  2. scopp
    Replies:
    3
    Views:
    569
    Guest
    Jan 22, 2004
  3. Bob
    Replies:
    5
    Views:
    16,957
    tuldom84
    Jul 15, 2012
  4. iksrazal@terra.com.br

    Reading file lines into large List

    iksrazal@terra.com.br, Sep 13, 2005, in forum: Java
    Replies:
    10
    Views:
    2,631
  5. Webster

    Reading lines from a file

    Webster, Jan 26, 2004, in forum: C++
    Replies:
    7
    Views:
    558
    Kevin Goodsell
    Jan 26, 2004
  6. sahm
    Replies:
    4
    Views:
    43,012
    rel0aded911
    Nov 23, 2009
  7. Murali
    Replies:
    2
    Views:
    783
    Jerry Coffin
    Mar 9, 2006
  8. varun7rs@gmail.com
    Replies:
    5
    Views:
    360
    Chris Angelico
    May 14, 2014
Loading...