Rya*_*ger 1 python string tuples stringio python-3.x
所以这是我的程序的第二个问题,但是一个完全不同的问题,感谢有帮助的人建议 JSON 作为做我想做的更好的方法......
反正...
JSON 取得了一些成功。该程序也更改了主题,我绝对不是要制作游戏,只是获得灵感来了解更多有关 python 中“保存”概念的信息。 .. 但我遇到了另一个问题,当我尝试使用 JSON 的 .dump 方法时它报告了这个错误
错误:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 32, in <module>
File "/data/data/com.hipipal.qpy3/files/lib/python3.2/python32.zip/json/__init__.py", line 177, in dump
io.UnsupportedOperation: not writable
Run Code Online (Sandbox Code Playgroud)
代码:
import os
import random
import json
with open("/storage/emulated/0/com.hipipal.qpyplus/scripts3/newgame2.txt") as data_file:
data = json.load(data_file)
save=data
print(save)
hero=dict(save)
print(hero)
level=int(0)
job=str("none")
experience=int(0)
strength=int(0)
intelligence=int(0)
dexterity= int(0)
health= int(0)
magic= int(0)
luck= int(0)
if hero["level"]==0:
level=int(0)
job=str("none")
experience=int(0)
strength=int(0)
intelligence=int(0)
dexterity= int(0)
health= int(0)
magic= int(0)
luck= int(0)
hero=[("level",level), ("job",job), ("experience",experience), ("strength",strength), ("intelligence",intelligence), ("dexterity",dexterity), ("health",health), ("magic",magic), ("luck",luck)]
hero=dict(hero)
with open("/storage/emulated/0/com.hipipal.qpyplus/scripts3/newgame2.txt") as data_file:
json.dump(hero, data_file)
Run Code Online (Sandbox Code Playgroud)
您不是在“写入模式”下打开文件。
尝试将您的open()
线路更改为:
with open("/storage/emulated/0/com.hipipal.qpyplus/scripts3/newgame2.txt", "w") as data_file:
json.dump(hero, data_file)
Run Code Online (Sandbox Code Playgroud)
默认情况下,Python 的内置函数open()
以“读取”模式打开文件。
mode 最常用的值是 'r' 用于读取,'w' 用于写入(如果文件已经存在,则截断文件)和 'a' 用于附加(在某些 Unix 系统上这意味着所有写入都附加到末尾文件的位置,而不管当前的查找位置)。如果省略 mode,则默认为 'r'。默认是使用文本模式,它可以在写入和读取时将 '\n' 字符转换为特定于平台的表示形式。因此,打开二进制文件时,应在模式值后附加“b”以二进制模式打开文件,这将提高可移植性。(即使在不以不同方式处理二进制文件和文本文件的系统上,附加 'b' 也很有用,它用作文档。)有关更多可能的模式值,请参见下文。
归档时间: |
|
查看次数: |
3378 次 |
最近记录: |