Python 从文本文件中读取多个数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12917588/
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
Reading multiple numbers from a text file
提问by slayeroffrog
I am new to programming in python and need help doing this.
我是 python 编程的新手,需要帮助。
I have a text file with several numbers like this:
我有一个包含多个数字的文本文件,如下所示:
12 35 21
123 12 15
12 18 89
I need to be able to read the individual numbers of each line to be able to use them in mathematical formulas.
我需要能够读取每行的单个数字才能在数学公式中使用它们。
采纳答案by mgilson
In python, you read a line from a file as a string. You can then work with the string to get the data you need:
在 python 中,你从文件中读取一行作为字符串。然后您可以使用字符串来获取您需要的数据:
with open("datafile") as f:
for line in f: #Line is a string
#split the string on whitespace, return a list of numbers
# (as strings)
numbers_str = line.split()
#convert numbers to floats
numbers_float = [float(x) for x in numbers_str] #map(float,numbers_str) works too
I've done it all in a bunch of steps, but you'll often see people combine them:
我已经通过一系列步骤完成了这一切,但您经常会看到人们将它们结合起来:
with open('datafile') as f:
for line in f:
numbers_float = map(float, line.split())
#work with numbers_float here
Finally, using them in a mathematical formula is easy too. First, create a function:
最后,在数学公式中使用它们也很容易。首先,创建一个函数:
def function(x,y,z):
return x+y+z
Now iterate through your file calling the function:
现在遍历调用函数的文件:
with open('datafile') as f:
for line in f:
numbers_float = map(float, line.split())
print function(numbers_float[0],numbers_float[1],numbers_float[2])
#shorthand: print function(*numbers_float)
回答by Daniel Thaagaard Andreasen
Another way to do it is by using numpy's function called loadtxt.
另一种方法是使用numpy调用的函数loadtxt。
import numpy as np
data = np.loadtxt("datafile")
first_row = data[:,0]
second_row = data[:,1]
回答by mou
This should work if you name your file numbers.txt
如果您将文件命名为 numbers.txt,这应该有效
def get_numbers_from_file(file_name):
file = open(file_name, "r")
strnumbers = file.read().split()
return map(int, strnumbers)
print get_numbers_from_file("numbers.txt")
this must return [12, 35, 21, 123, 12, 15, 12, 18, 89] after you can choose individuly every numbers with list_variable[intergrer]
这必须返回 [12, 35, 21, 123, 12, 15, 12, 18, 89] 在您可以单独选择每个数字后, list_variable[interger]
回答by karpanai
The following code should work
下面的代码应该工作
f = open('somefile.txt','r')
arrayList = []
for line in f.readlines():
arrayList.extend(line.split())
f.close()
回答by Stefan Gruenwald
If you want to use the file name as your argument at the command line, then you can do the following:
如果要在命令行中使用文件名作为参数,则可以执行以下操作:
from sys import argv
input_file = argv[1]
with open(input_file,"r") as input_data:
A= [map(int,num.split()) for num in input_data.readlines()]
print A #read out your imported data
otherwise you can do this:
否则你可以这样做:
from os.path import dirname
with open(dirname(__file__) + '/filename.txt') as input_data:
A= [map(int,num.split()) for num in input_data.readlines()]
print A

