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?
Python enumerate() Function The enumerate() function takes a collection (e.g. a tuple) and returns it as an enumerate object. The enumerate() function adds a counter as the key of the enumerate object.
To enumerate directories and files, use methods that return an enumerable collection of directory or file names, or their DirectoryInfo, FileInfo, or FileSystemInfo objects. If you want to search and return only the names of directories or files, use the enumeration methods of the Directory class.
Overview. The io module provides Python's main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These are generic categories, and various backing stores can be used for each of them.
What does enumerate do in Python? The enumerate function in Python converts a data collection object into an enumerate object. Enumerate returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access.
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With