[英]I get this error: ValueError: invalid literal for int() with base 10: '\n'
I have a part inside my code where I receive data from a text file, this is that part: last_score_file = open("/Users/lvanrem/PythonTest/last_score2", "r")我的代码中有一部分从文本文件接收数据,这就是那部分:last_score_file = open("/Users/lvanrem/PythonTest/last_score2", "r")
last_score_temprature = int(last_score_file.readline(1))
last_score_guess = int(last_score_file.readline(2))
add_to_random = int(last_score_file.readline(3))
last_score_file.close
it requests date from this file:它从这个文件中请求日期:
0
0
0
and it gives this error它给出了这个错误
ValueError: invalid literal for int() with base 10: '\n' ValueError: int() 以 10 为底的无效文字:'\n'
let me know if you can help me...让我知道你是否可以帮助我...
trim endline from read value从读取值修剪端线
last_score_temprature = int(last_score_file.readline(1).strip())
last_score_guess = int(last_score_file.readline(2).strip())
add_to_random = int(last_score_file.readline(3).strip())
last_score_file.close
The line you read has \n
in it so the value is incorrect.您阅读的行中包含
\n
,因此该值不正确。 The 'readline(x)' read the next x characters so your out put will be like '0, \n, 0\n' Try: 'readline(x)' 读取下一个 x 字符,因此您的输出将类似于 '0, \n, 0\n' 尝试:
with open("/Users/lvanrem/PythonTest/last_score2", "r") as f:
last_score_temprature = int(f.readline())
last_score_guess = int(f.readline())
add_to_random = int(f.readline())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.