python json转储可写性“无法写" [英] python json dump writability "not write able"

查看:253
本文介绍了python json转储可写性“无法写"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我程序的第二个问题,但又是一个完全不同的问题,这要归功于乐于助人的人,他建议将JSON作为一种更好的方法来实现我想要的....

So this is the second piece of a problem with my program, but a completely different issue, thanks to the helpful person who suggested JSON as a better method to do what I wanted....

反正...

JSON取得了一些成功.该程序还改变了主题,我绝对不打算制作游戏,只是获得灵感来学习更多有关python中保存"的概念的知识.所以这是到目前为止的代码,其中包含一个可读取的有效JSON编码文件..但我遇到另一个障碍,当我尝试使用JSON的.dump方法时,它会报告此错误

some success with JSON. The program also changed theme, I definitely am not tying to make a game, just get the inspiration to learn more about the concept of "saving" in python.. so here's my code so far, with a valid JSON coded file to read from.. but I ran into another snag, it reports this error when I try to use the JSON 's .dump method

错误:

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

代码:

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)

推荐答案

您未在写入模式"下打开文件.

You are not opening your file in "write mode".

尝试将您的open()行更改为此:

Try to change your open() line to this:

with open("/storage/emulated/0/com.hipipal.qpyplus/scripts3/newgame2.txt", "w") as data_file:
    json.dump(hero, data_file)

默认情况下,Python的内置 open() 在读取"中打开文件"模式.

By default Python's builtin open() opens files in "read" mode.

最常用的mode值是'r'表示读取,'w'表示 写入(如果文件已经存在,则将其截断),并用'a'表示 附加(在某些Unix系统上,这意味着所有写入都附加到 不论当前搜寻位置在文件末尾). 如果模式 省略,默认为"r".默认为使用文本模式,即 可以将'\ n'字符转换为特定于平台的表示形式 写作并重新阅读.因此,当打开一个二进制文件时, 应该在模式值后附加"b"以以二进制模式打开文件, 这将提高可移植性. (即使在 不会在二进制文件和文本文件上以不同方式处理的系统 作为文档.)有关mode的更多可能值,请参见下文.

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.

这篇关于python json转储可写性“无法写"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆