10

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?

1
  • 1
    This question has been asked (several times) before (stackoverflow.com/questions/2081836/…) 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. Commented Jun 24, 2011 at 20:54

2 Answers 2

15

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'

Sign up to request clarification or add additional context in comments.

5 Comments

strip() is unnecessary, as only readlines() adds the trailing newline.
it is in this case. see my updated answer that addresses this.
the 'return code' should be before the break? I thought return was always supposed to be the last line of a function..?
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.
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.
10

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.

3 Comments

+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 =(
I often do answer questions in that terse way, but sometimes it seems clearly necessary to talk people through things.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.