如何使用python读取文本文件中的数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21285684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:28:17  来源:igfitidea点击:

How to read numbers in text file using python?

pythonfile-io

提问by user3215074

I am new to python programming and I am learning python by doing simple programs. Here is what I would like to do: if I have a text file containing numbers: say this a f1.txt

我是 python 编程的新手,我正在通过做简单的程序来学习 python。这是我想要做的:如果我有一个包含数字的文本文件:说这是一个 f1.txt

f1.txt:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 15


fp = open('f1.txt')
a1=[]
a2=[]
a3=[]
a4=[]
lines = fp.readlines()

for ln in lines[0:len(lines)]:
line=ln.strip().split()
a1=line();

fp.close()

I want to get first column in a1, second in a2 and so on. I know above code may be wrong, please tell me where I went wrong and how to correct it. Especially I am not understanding command 'ln.strip().split()'. Can someone help?

我想在 a1 中获得第一列,在 a2 中获得第二列,依此类推。我知道上面的代码可能是错误的,请告诉我哪里出错了以及如何更正。特别是我不理解命令'ln.strip().split()'。有人可以帮忙吗?

采纳答案by poke

You could do it like this:

你可以这样做:

a1 = []
a2 = []
a3 = []
a4 = []

with open('f1.txt') as f:
    for line in f:
        data = line.split()
        a1.append(int(data[0]))
        a2.append(int(data[1]))
        a3.append(int(data[2]))
        a4.append(int(data[3]))

So first of all, we use the withstatement to open the file. This makes sure that the file is automatically closed even when errors appear. It's just nicer that way. While the file is open fwill be the file handle.

所以首先,我们使用with语句打开文件。这确保即使出现错误,文件也会自动关闭。这样更好。文件打开时f将是文件句柄。

Now, Python allows us to iterate over the lines of a file simply by iterating over the file handle. So for line in fwill iterate over all lines automatically. There is no need to call readlines()first, and certainly no need to do lines[0:len(lines)]which essentially only creates a copy of the list—you could just iterate over linestoo.

现在,Python 允许我们通过简单地遍历文件句柄来遍历文件的行。所以for line in f会自动迭代所有行。不需要先调用readlines(),当然也不需要这样做lines[0:len(lines)],这基本上只创建列表的副本——你也可以迭代lines

Now inside of the loop, we take the line, and split it by whitespace—without arguments str.splitwill always do that. str.splitreturns a list, so we store that in an extra variable. Next we append each column to the correct list. And as you want the values as numbers, we convert them to integers.

现在在循环内部,我们取一行,并用空格分割它——没有参数str.split总是这样做的。str.split返回一个列表,因此我们将其存储在一个额外的变量中。接下来,我们将每一列附加到正确的列表中。当您希望将值作为数字时,我们将它们转换为整数。

The str.stripyou mentioned basically takes off any leading or trailing whitespace of the string. As we are using str.splitwithout arguments, extra whitespace will be removed too, so we don't really need that.

str.strip你提到的基本上起飞字符串的开头或结尾的空白。由于我们在str.split不带参数的情况下使用,额外的空格也将被删除,所以我们并不真正需要它。

Finally, having four separate lists stored in separate variables is a bit annoying to maintain. You could simply create a list of lists instead:

最后,将四个单独的列表存储在单独的变量中维护起来有点烦人。您可以简单地创建一个列表列表:

a = [[], [], [], []] # A list with four empty lists

And then, inside of the loop, you can just append data[i]to a[i]:

然后,在循环内部,您可以附加data[i]a[i]

for i, value in enumerate(line.split()):
    a[i].append(int(value))

When iterating over enumerate, you will not only get the value (which you would get when iterating just over the list), but also the index. So using this, we get the index of each element within the splitted line and can automatically append it to the correct sublist of a.

迭代时enumerate,您不仅会得到值(在列表上迭代时会得到),还会得到索引。因此,使用它,我们获得了分割线内每个元素的索引,并可以自动将其附加到 的正确子列表中a

回答by dornhege

line[0], line[1], etc. should give you the first, second, etc. entry in each line.

line[0], line[1]等应该为您提供每行中的第一个、第二个等条目。

The split()function will split the given line at whitespace and returns a list of the entries.

split()函数将在空白处拆分给定的行并返回条目列表。

回答by kylie.a

Your indentation is wrong in the forloop. All the code that you want included in the loop should be indented 4 spaces.

您的缩进在for循环中是错误的。您希望包含在循环中的所有代码都应缩进 4 个空格。

The line a1= line()won't do anything. The syntax a = A()would set aequal to the result of a function A()or to a new instance of a class A. If you want to add line to the list a1you need to use a1.append(line)

该行a1= line()不会做任何事情。语法a = A()将设置为a等于函数的结果A()或类的新实例A。如果要将行添加到列表中a1,则需要使用a1.append(line)

回答by volcano

data = []
for line in lines:
    data.append([int(v) for v in line.split()])

or

或者

data = [[int(v) for v in line.split()] for line in lines]

EDIT:To answer the comment - code below will rearrange the data as required list of numbers

编辑:要回答评论 - 下面的代码将根据需要的数字列表重新排列数据

numbers = zip(*data)