简体   繁体   English

Python tinydb - 如何获取特定元素名称?

[英]Python tinydb - How to get specific element name?

This is what my structure looks like:这是我的结构的样子:

{'MovieName': 'Its-a-Wonderful-Life', 'Description': 'MovieDiscription', 'IMDBID': '0038650'}

I want to print out only the MovieName element.我只想打印出 MovieName 元素。

I'm using this code to get it:我正在使用此代码来获取它:

db = TinyDB('Databases/Downloaded_Movies.json')

for item in db:

   print(item.MovieName)

but I'm getting this error:但我收到此错误:

 AttributeError: 'Document' object has no attribute 'MovieName'

How do I fix the code?如何修复代码?

TinyDB expects an indexed table document, not a list. TinyDB 需要一个索引表文档,而不是一个列表。 Unless you want to write a custom middleware for your TinyDB, you'll either have to modify your JSON除非您想为 TinyDB 编写自定义中间件,否则您必须修改 JSON

"1 ": {'MovieName': 'Its-a-Wonderful-Life', 'Description': 'MovieDiscription', 'IMDBID': '0038650'}

or you can try to print the element with indexes as或者您可以尝试将带有索引的元素打印为

import json
wjson = db.read()    
wjdata = json.loads(wjson)
print wjdata['1'][0]['Moviename']

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM