[英]Reading multiple variables from a text file in python 3.3?
I have a text file in the same directory labelled test.txt. 我在同一目录中有一个文本文件,标记为test.txt。 It contains the following 3 lines:
它包含以下3行:
10
3
5
The output this gives me is: 这给我的输出是:
1
0
3
My code is below. 我的代码如下。
f = open('test.txt',"r")
test1 =(f.readline(1))
test2 = (f.readline(2))
test3 = (f.readline(3))
print (test1)
print (test2)
print (test3)
How would i go about making it pick up the full variables? 我将如何处理所有变量? Ie test1 = 10, test2=3, test3 = 5?
即test1 = 10,test2 = 3,test3 = 5? Very new to python :(
python非常新:(
You don't have to give f.readline()
an argument at all: 您根本不必给
f.readline()
一个参数:
test1 = f.readline()
test2 = f.readline()
test3 = f.readline()
otherwise you limit the number of characters read. 否则,您将限制读取的字符数。
f.readline(1)
does not mean 'read line 1'; f.readline(1)
并不意味着'读线1'; instead you say: 'read a line, but no more than 1 character should be read'. 相反,您说:“读取一行,但读取的字符数不能超过1个”。
Quoting from the IOBase.readline()
docs : 引用
IOBase.readline()
docs :
Read and return one line from the stream.
从流中读取并返回一行。 If size is specified, at most size bytes will be read.
如果指定了大小,则最多将读取大小字节。
Emphasis mine. 强调我的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.