简体   繁体   English

将字符串从 txt 文件转换为 integer 时,出现 ValueError: invalid literal for int() with base 10:

[英]When converting string to integer from txt file, I get ValueError: invalid literal for int() with base 10:

I am trying to create an external highscore file.我正在尝试创建一个外部高分文件。

highscoreFile = open("highscore.txt", "r+")
highscore = highscoreFile.read()

if current > int(highscore):
    highscoreFile.truncate(0)
    print(highscore)

It works if highscore.txt has only a 0, but if it is any number I get the error.如果 highscore.txt 只有 0,它会工作,但如果它是任何数字,我就会收到错误。 If I check the value of highscore right before the if statement (using pycharm debug), it gives me a string value of what the high score is, which is what I would expect.如果我在 if 语句之前检查 highscore 的值(使用 pycharm 调试),它会给我一个表示最高分的字符串值,这正是我所期望的。 Though, whent trying to print it, says it is n.netype.虽然,当试图打印它时,说它是 n.netype。 Why is it giving me this error?为什么会给我这个错误?

For some reason the script created spaces before the number in the file.由于某种原因,脚本在文件中的数字之前创建了空格。

暂无
暂无

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

相关问题 Discord.py音乐bot,使用文本文件保存音量整数,但我得到ValueError:int()的无效文字,基数为10:” - Discord.py music bot, using a text file to save volume integer but I get ValueError: invalid literal for int() with base 10: '' 当我插入整数时,为什么会收到“ ValueError:int()以10为底的无效文字:” - why do I recieve “ValueError: invalid literal for int() with base 10: ''” when I do insert integer ValueError: int() 以 10 为底的无效文字:'C',从字符串到 integer 的转换发生在哪里? - ValueError: invalid literal for int() with base 10: 'C' , Where is the conversion from string into integer happening? ValueError:int() 的无效文字,基数为 10:'string' - ValueError: invalid literal for int() with base 10: 'string' 我收到此错误: ValueError: invalid literal for int() with base 10: '\n' - I get this error: ValueError: invalid literal for int() with base 10: '\n' 我通常会收到此错误: ValueError: invalid literal for int() with base 10 - i usually get this error : ValueError: invalid literal for int() with base 10 Kaprekar数字:我得到ValueError:以10为底的int()的无效文字'' - Kaprekar numbers: I Get ValueError: invalid literal for int() with base 10 '' 当我运行以下代码时,出现以下错误: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')” ValueError:int() 的无效文字,基数为 10:'FALSE' 从数据集中删除空字符串时 - ValueError: invalid literal for int() with base 10: 'FALSE' When removing empty string from a dataset 拆分文本时出现“ValueError:int() 以 10 为基数的无效文字:''” - I get “ValueError: invalid literal for int() with base 10: ''” when splitting text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM