简体   繁体   English

带枚举的基本python file-io变量

[英]Basic python file-io variables with enumerate

New to python and trying to learn the ropes of file i/o. python的新手,并试图学习文件i / o的绳索。

I am working with pulling lines from a large (2 million line) file in this format: 我正在使用以下格式从大型(200万行)文件中提取行:

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? 一旦索引到达正确的位置(i),我使用什么语法来设置代码变量?

code = line.strip()

will assign code to the line number that is equal to i while removing the trailing new line. 将删除尾随的新行时,将代码分配给等于i的行号。

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() 为什么你需要.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' 是的,“'LINGUISTIC AFFILIATION',”是我目前的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). enumerate将一个迭代器转换为另一个迭代器,这样你迭代的东西就成了一对(数字ID,来自底层迭代器的原始项)。

In our case: 在我们的情况下:

for index, line in enumerate(f):

f is the file object. f是文件对象。 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). for循环语法遍历迭代器,并将(行号,文件行)对分配给变量:(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. 因此,每次循环时,都会为index分配一个行号,并从该文件中为line分配相应的行。 The line from the file is what you want (possibly with some formatting), and line contains it, so... 文件中的行是你想要的(可能有一些格式), line包含它,所以......

If any of the above didn't make sense, you probably need to re-read an introduction to the language. 如果上述任何一个没有意义,您可能需要重新阅读该语言的介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM