Python:访问位于文本文件内部的字典
我正在编写一个快速而肮脏的脚本来获取 Chromium 的书签并将它们变成 Openbox 的管道菜单。 Chromium 将其书签存储在一个名为 Bookmarks 的文件中,该文件以字典形式存储信息,如下所示:
{ "checksum": "99999999999999999999", "roots": { "bookmark_bar": { "children": [ { "date_added": "9999999999999999999", "id": "9", "name": "Facebook", "type": "url", "url": "http://www.facebook.com/" }, { "date_added": "999999999999", "id": "9", "name": "Twitter", "type": "url", "url": "http://twitter.com/"
How will I open this Dictionary in this file in Python并将其分配给变量。我知道您使用 open()
打开一个文件,但我真的不知道从哪里开始。最后,我希望能够从这样的变量访问字典中的信息 bookmarks[bookmarks_bar][children][0][name]
并让它返回 'Facebook '
I am working on a quick and dirty script to get Chromium's bookmarks and turn them into a pipe menu for Openbox. Chromium stores it's bookmarks in a file called Bookmarks that stores information in a dictionary form like this:
{ "checksum": "99999999999999999999", "roots": { "bookmark_bar": { "children": [ { "date_added": "9999999999999999999", "id": "9", "name": "Facebook", "type": "url", "url": "http://www.facebook.com/" }, { "date_added": "999999999999", "id": "9", "name": "Twitter", "type": "url", "url": "http://twitter.com/"
How would I open this dictionary in this file in Python and assign it to a variable. I know you open a file with open()
, but I don't really know where to go from there. In the end, I want to be able to access the info in the dictionary from a variable like this bookmarks[bookmarks_bar][children][0][name]
and have it return 'Facebook'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你知道这是不是一个json文件吗?如果是这样,python 提供一个 json 库。
Json 可以用作数据序列化/交换格式。这很好,因为它是跨平台的。像你问的那样导入它似乎相当容易,文档中的一个例子:
所以在你的情况下它看起来像:
Do you know if this is a json file? If so, python provides a json library.
Json can be used as a data serialization/interchange format. It's nice because it's cross platform. Importing this like you ask seems fairly easy, an example from the docs:
So in your case it would look something like:
JSON 绝对是执行此操作的“正确”方法,但对于快速而肮脏的脚本
eval()
可能就足够了:JSON is definitely the "right" way to do this, but for a quick-and-dirty script
eval()
might suffice: