简体   繁体   English

如何为JSON字符串对象赋予要读取的属性?

[英]How do I give a JSON string object an attribute to read?

I'm trying to use JSON to select objects from a list, then delete those objects once they've been selected. 我正在尝试使用JSON从列表中选择对象,然后在选择这些对象后将其删除。 I keep running into errors telling me 我不断遇到错误告诉我

AttributeError: 'str' object has no attribute 'read'

错误消息,json文件,原始代码

path_album_list = 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python36\\albums.json'
albums = json.load(path_album_list)
album = random.choice(albums)
print ('Today\'s soundtrack is "%s."' % album)
albums.remove(album)
json.dump(albums, path_album_list)

I also tried using json.loads , but then I got even more errors: 我也尝试使用json.loads ,但是随后出现了更多错误:

Traceback (most recent call last):
  File "C:\Users\steve\AppData\Local\Programs\Python\Python36\randomizer.py", line 20, in <module>
    albums = json.loads(path_album_list)
  File "C:\Users\steve\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\steve\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\steve\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)*

json.load takes a file object, not a string. json.load使用文件对象,而不是字符串。 So you need to do this: 因此,您需要这样做:

path_album_list = 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python36\\albums.json'
jsonfile = open(path_album_list, "r")
albums = json.load(jsonfile)
jsonfile.close()
# ...
jsonfile = open(path_album_list, "w")
json.dump(albums, jsonfile)
jsonfile.close()

Or, you can use with statements, but the above is probably easier to understand. 或者,您可以使用with语句,但是上面的内容可能更容易理解。

path_album_list = 'C:\\Users\\steve\\AppData\\Local\\Programs\\Python\\Python36\\albums.json'
with open(path_album_list, "r") as jsonfile:
    albums = json.load(jsonfile)
# ...
with open(path_album_list, "w") as jsonfile:
    json.dump(albums, jsonfile)

The reason you need to open the files is that json interacts with files, not with strings of the file path. 您需要open文件的原因是json与文件交互,而不是与文件路径的字符串交互。

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

相关问题 如何在 python 中读取 JSON object? - How do I read an JSON object in python? 如何在Python中为字符串对象提供自定义方法? - How do I give a string object a custom method in Python? 如何使用字符串在列表中引用 object 的属性? - How do I refer to an attribute of an object in a list, by using a string? 如何将字符串(类似于json)转换为json对象? - How do I convert a string (which is like json) to a json object? 如何给模拟对象命名? - How do I give name to a mock object? 如果属性名称包含在字符串中,如何从对象获取属性? - How do I get an attribute from an object if I have the attribute name in a string? 给定一个描述object.attribute的Python字符串,如何将属性的名称空间与属性分开? - Given a Python string describing object.attribute, how do I separate the attributes's namespace from the attribute? 我收到一条错误消息,指出 AttributeError: &#39;str&#39; object has no attribute &#39;read&#39;,我不知道如何修复它 - I'm getting an error that says AttributeError: 'str' object has no attribute 'read' and i do not know how to fix it 字符串对象没有读取属性 - String object has no attribute read 当 str 对象属性为“只读”时,这意味着什么? 如何修复我的代码? - What does it mean when a str object attribute is "read-only?" How do I fix my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM