我试图将名为“curmov”的 int 保存到一个文本文件中,以便以后可以再次访问它。我不断收到 TypeError: must be str, not int. 我需要它保持完整。任何想法
curmov = 21
def savemov():
global curmov
text_file = open("curmov.txt", "w+")
text_file.write(curmov)
text_file.close()
Run Code Online (Sandbox Code Playgroud)
将其转换为字符串
text_file.write(str(curmov))
Run Code Online (Sandbox Code Playgroud)
您仍然可以在您的函数中使用curmovas int。这只会制作一个临时string副本并将其写入文件。就文件而言,它不知道它是int float还是duck,一切都是字符串!
| 归档时间: |
|
| 查看次数: |
2475 次 |
| 最近记录: |