簡體   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:

我正在嘗試創建一個外部高分文件。

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

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

如果 highscore.txt 只有 0,它會工作,但如果它是任何數字,我就會收到錯誤。 如果我在 if 語句之前檢查 highscore 的值(使用 pycharm 調試),它會給我一個表示最高分的字符串值,這正是我所期望的。 雖然,當試圖打印它時,說它是 n.netype。 為什么會給我這個錯誤?

由於某種原因,腳本在文件中的數字之前創建了空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM