简体   繁体   English

我收到此错误: ValueError: invalid literal for int() with base 10: '\n'

[英]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.

相关问题 我通常会收到此错误: ValueError: invalid literal for int() with base 10 - i usually get this error : ValueError: invalid literal for int() with base 10 ValueError:int()以10为底的无效文字:'\\ n' - ValueError: invalid literal for int() with base 10: '\n' 错误 - ValueError:基数为10的int()的文字无效:'' - Error - ValueError: invalid literal for int() with base 10: ' ' 错误:ValueError:int()以10为底的无效文字: - Error: ValueError: invalid literal for int() with base 10: '' ValueError:int() 的无效文字,基数为 10:'' 错误 - ValueError: invalid literal for int() with base 10: '' error Kaprekar数字:我得到ValueError:以10为底的int()的无效文字'' - Kaprekar numbers: I Get ValueError: invalid literal for int() with base 10 '' 我不断收到此错误: ValueError: invalid literal for int() with base 10: '' - I keep getting this error: ValueError: invalid literal for int() with base 10: '' ValueError:int()以10为底的无效文字:b'1 \\ n5 \\ n' - ValueError: invalid literal for int() with base 10: b'1\n5\n' 错误:n=int(input()) ValueError:以 10 为底的 int() 的无效文字:'N/A' - error :n=int(input()) ValueError: invalid literal for int() with base 10: 'N/A' 当我运行以下代码时,出现以下错误:ValueError:int()以10为底的无效文字:“((1,0,'Friday')” - When I run the following code,I get this error:ValueError: invalid literal for int() with base 10: “(1, 0, 'Friday')”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM