8

New to python and trying to learn the ropes of file i/o.

I am working with pulling lines from a large (2 million line) file in this format:

56fr4
4543d
4343d
5irh3

Here is the function I'm using to return a code:

def getCode(i):
    with open("test.txt") as f:
        for index, line in enumerate(f):
            if index == i:
                code = # what does it equal?
                break
    return code

Once the index gets to the right spot (i), what syntax do I use to set the code variable?

Jindra Helcl
  • 3,457
  • 1
  • 20
  • 26
some1
  • 2,447
  • 8
  • 26
  • 23
  • 1
    This question has been asked (several times) before (http://stackoverflow.com/questions/2081836/reading-specific-lines-only-python) and there are some good answers, with discussion; some use enumerate and some linecache (which may be faster). There are also some bad answers, which is itself educational. – smci Jun 24 '11 at 20:54

2 Answers2

13

code = line.strip()

will assign code to the line number that is equal to i while removing the trailing new line.

you also need to update your code a bit

 def getCode(i):
    with open('temp.txt', 'r') as f:
             for index, line in enumerate(f):
                     if index == i:
                             code = line.strip()
                             return code

why you need .strip()

>>> def getCode(i):
...     with open('temp.txt') as f:
...             for index, line in enumerate(f):
...                     if index == i:
...                             code = line
...                             return code
 ... 
>>> getCode(2)
"                  'LINGUISTIC AFFILIATION',\n"

yes, " 'LINGUISTIC AFFILIATION'," is in my current temp.txt'

matchew
  • 19,195
  • 5
  • 44
  • 48
  • strip() is unnecessary, as only readlines() adds the trailing newline. – Steve Howard Jun 24 '11 at 20:18
  • it is in this case. see my updated answer that addresses this. – matchew Jun 24 '11 at 20:28
  • 1
    the 'return code' should be before the break? I thought return was always supposed to be the last line of a function..? – some1 Jun 24 '11 at 20:29
  • 1
    well, if you break before returning code, code wont be returned, and if you return code outside of the `if` you run the risk of code never being defined. Try it. I just did before answering. you could define and `else` or a `try` to return the code at the end, but this is perfectly proper. – matchew Jun 24 '11 at 20:33
  • 3
    You can `return` from anywhere within a function. (Whether you **should** is a bigger argument; try to do things that you can understand easily.) The `break` here is unnecessary, and can never be reached. As soon as a 'return' is encountered, that call of the function stops doing any more work and returns whatever value. – Karl Knechtel Jun 24 '11 at 20:38
8

enumerate transforms one iterator into another, such that the things you iterate over become pairs of (numeric ID, original item from the underlying iterator).

In our case:

for index, line in enumerate(f):

f is the file object. File objects are iterators: they iterate over lines of the file.

We transform that into an iterator over (line number, line from file) pairs.

The for loop syntax iterates over the iterator, and assigns the (line number, line from file) pair to the variables: (index, line). This is just the normal behaviour of assigning one tuple to another.

So, each time through the loop, index gets assigned a line number, and line gets assigned the corresponding line from the file. The line from the file is what you want (possibly with some formatting), and line contains it, so...

If any of the above didn't make sense, you probably need to re-read an introduction to the language.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 2
    +1 thanks for taking the time to explain `enumerate` and not just giving him 'what he needs' as I did. or copy a link to the documentation like someone else did. I saw someone earlier answer a question with simply 'sudo' and having it link to wikipedia =( – matchew Jun 24 '11 at 20:48
  • I often do answer questions in that terse way, but sometimes it seems clearly necessary to talk people through things. – Karl Knechtel Jun 24 '11 at 20:50
  • 1
    I am trying to do basic scripts along the way as I learn. If I just "read an introduction" it doesn't stick whatsoever, so I try to read a little, then do a small project, and so on. Is there somewhere on the board that I can ask my easier level questions without people getting annoyed by it? I read the enumerate documentation several times, but it didn't make sense to me the way it was worded. – some1 Jun 24 '11 at 20:54