Python:访问位于文本文件内部的字典

发布于 2024-11-19 19:31:34 字数 825 浏览 1 评论 0原文

我正在编写一个快速而肮脏的脚本来获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-11-26 19:31:34

你知道这是不是一个json文件吗?如果是这样,python 提供一个 json 库。

Json 可以用作数据序列化/交换格式。这很好,因为它是跨平台的。像你问的那样导入它似乎相当容易,文档中的一个例子:

>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]

所以在你的情况下它看起来像:

import json
with open(file.txt) as f:
    text = f.read()
    bookmarks = json.loads(text)
print bookmarks[bookmarks_bar][children][0][name]

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:

>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]

So in your case it would look something like:

import json
with open(file.txt) as f:
    text = f.read()
    bookmarks = json.loads(text)
print bookmarks[bookmarks_bar][children][0][name]
孤独患者 2024-11-26 19:31:34

JSON 绝对是执行此操作的“正确”方法,但对于快速而肮脏的脚本 eval() 可能就足够了:

with open('file.txt') as f:
    bookmarks = eval(f.read())

JSON is definitely the "right" way to do this, but for a quick-and-dirty script eval() might suffice:

with open('file.txt') as f:
    bookmarks = eval(f.read())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文