简体   繁体   English

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

[英]How to read numbers in text file using python?

I am new to python programming and I am learning python by doing simple programs. 我是python编程的新手,我正在通过编写简单的程序学习python。 Here is what I would like to do: if I have a text file containing numbers: say this a f1.txt 这是我要执行的操作:如果我有一个包含数字的文本文件:将此说为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. 我想在a1中获得第一列,在a2中获得第二列,依此类推。 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()'. 特别是我不理解命令'ln.strip()。split()'。 Can someone help? 有人可以帮忙吗?

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 with statement to open the file. 因此,首先,我们使用with语句打开文件。 This makes sure that the file is automatically closed even when errors appear. 这样可以确保即使出现错误也可以自动关闭文件。 It's just nicer that way. 这样更好。 While the file is open f will be the file handle. 打开文件时, f将是文件句柄。

Now, Python allows us to iterate over the lines of a file simply by iterating over the file handle. 现在,Python允许我们仅通过遍历文件句柄来遍历文件的各行。 So for line in f will iterate over all lines automatically. 因此, for line in f它将自动遍历所有行。 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 lines too. 不需要先调用readlines() ,当然也不需要做lines[0:len(lines)] ,这实际上只会创建列表的副本-您也可以遍历lines

Now inside of the loop, we take the line, and split it by whitespace—without arguments str.split will always do that. 现在,在循环内部,我们str.split行代码分成空白并用空格分隔-不带参数str.split将始终执行该操作。 str.split returns a list, so we store that in an extra variable. str.split返回一个列表,因此我们将其存储在一个额外的变量中。 Next we append each column to the correct list. 接下来,我们将每一列附加到正确的列表中。 And as you want the values as numbers, we convert them to integers. 当您希望将值转换为数字时,我们会将其转换为整数。

The str.strip you mentioned basically takes off any leading or trailing whitespace of the string. 您提到的str.strip基本上去除了字符串的任何前导或尾随空格。 As we are using str.split without arguments, extra whitespace will be removed too, so we don't really need that. 当我们使用不带参数的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. enumerate迭代时,您不仅将获得值(仅在列表上进行迭代时将获得该值),还将获得索引。 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 . 所以用这个,我们得到的劈裂线中的每个元素的索引并可以自动追加到正确的子表a

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)

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()函数将在空格处分隔给定的行,并返回条目列表。

Your indentation is wrong in the for loop. 您的缩进在for循环中是错误的。 All the code that you want included in the loop should be indented 4 spaces. 您希望包含在循环中的所有代码都应缩进4个空格。

The line a1= line() won't do anything. a1= line()不会执行任何操作。 The syntax a = A() would set a equal to the result of a function A() or to a new instance of a class A . 语法a = A()会设定a等于一个函数的结果A()或一类的新实例A If you want to add line to the list a1 you need to use a1.append(line) 如果要将行添加到列表a1 ,则需要使用a1.append(line)

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

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