【问题标题】:Modify existing json to create new custom one python修改现有json创建新的自定义一python
【发布时间】:2022-08-14 15:34:29
【问题描述】:

我正在尝试修剪 json 中未使用的数据以创建只有两个字段的新数据。标题和描述。标题效果很好,但我不知道如何获取描述字段。 json 是公开的,您可以在here 或在帖子末尾获取它。

我提取标题字段的代码:

import requests
import json

def trim_json(d):
    newd = {}
    for name in [\'title\']:
        newd[name] = d[name]
    return newd

def clean():
    books = requests.get(\'https://openlibrary.org/authors/OL23919A/works.json\')
    books_parsed = books.json()
    book_data = books_parsed[\'entries\']
  
    book_data = [trim_json(d) for d in book_data]
    print(book_data)
    return book_data

更新

clean 函数以这种格式返回字典列表:

[{\'title\': \'Harry Potter House Gryffindor Edition Series 1-5 Books Collection Set By J.K. Rowling\'}]

我想要得到的是:

[{\'title\': \'Harry Potter House Gryffindor Edition Series 1-5 Books Collection Set By J.K. Rowling\', \'description\': \'lorem ipsum\'}]

如果没有描述:

[{\'title\': \'Harry Potter House Gryffindor Edition Series 1-5 Books Collection Set By J.K. Rowling\', \'description\': \'undefind\'}]

如何获取返回标题和描述的 json?

    {
        \"type\": {
            \"key\": \"/type/work\"
        },
        \"title\": \"Journey to Hogwarts\",
        \"authors\": [
            {
                \"type\": {
                    \"key\": \"/type/author_role\"
                },
                \"author\": {
                    \"key\": \"/authors/OL23919A\"
                }
            }
        ],
        \"covers\": [
            2520429
        ],
        \"key\": \"/works/OL28602152W\",
        \"latest_revision\": 1,
        \"revision\": 1,
        \"created\": {
            \"type\": \"/type/datetime\",
            \"value\": \"2022-08-05T00:16:59.602176\"
        },
        \"last_modified\": {
            \"type\": \"/type/datetime\",
            \"value\": \"2022-08-05T00:16:59.602176\"
        }
    },
    {
        \"description\": \"Harry Potter #2\\r\\n\\r\\nThroughout the summer holidays after his first year at Hogwarts School of Witchcraft and Wizardry, Harry Potter has been receiving sinister warnings from a house-elf called Dobby.\\r\\n\\r\\nNow, back at school to start his second year, Harry hears unintelligible whispers echoing through the corridors.\\r\\n\\r\\nBefore long the attacks begin: students are found as if turned to stone.\\r\\n\\r\\nDobby’s predictions seem to be coming true.\\r\\n\\r\\n[Source][1]\\r\\n\\r\\n\\r\\n  [1]: https://www.jkrowling.com/book/harry-potter-chamber-secrets/\",
        \"links\": [
            {
                \"title\": \"Author\'s book page\",
                \"url\": \"https://www.jkrowling.com/book/harry-potter-chamber-secrets/\",
                \"type\": {
                    \"key\": \"/type/link\"
                }
            },
            {
                \"url\": \"https://en.wikipedia.org/wiki/Harry_Potter_and_the_Chamber_of_Secrets\",
                \"title\": \"Wikipedia entry\",
                \"type\": {
                    \"key\": \"/type/link\"
                }
            },
            {
                \"title\": \"Harry Potter and the Chamber of Secrets by J.K. Rowling - review | Children\'s books | The Guardian\",
                \"url\": \"https://www.theguardian.com/childrens-books-site/2015/mar/02/review-j-k-rowling-harry-potter-chamber-secrets\",
                \"type\": {
                    \"key\": \"/type/link\"
                }
            },
            {
                \"url\": \"https://www.theguardian.com/childrens-books-site/2016/may/26/harry-potter-and-the-chamber-of-secrets-jk-rowling-review\",
                \"title\": \"Harry Potter and the Chamber of Secrets by J.K. Rowling - review 2 | Children\'s books | The Guardian\",
                \"type\": {
                    \"key\": \"/type/link\"
                }
            }
        ],
        \"title\": \"Harry Potter and the Chamber of Secrets\",
        \"covers\": [
            8234423,
            8237628,
            8237644,
            8392798,
            8995302,
            8762432,
            8081272,
            8353396,
            10301720,
            8938317,
            10471286,
            10413455,
            10487260,
            -1,
            10535729,
            10722535,
            10722534,
            11522289,
            12347254,
            12581306,
            12606939,
            10536577,
            11540339,
            12023623
        ],
        \"subject_places\": [
            \"England\",
            \"London\",
            \"Hogwarts School of Witchcraft and Wizardry\",
            \"Inglaterra\",
            \"Privet Drive\"
        ],
        \"subjects\": [
            \"Fantasy fiction\",
            \"school stories\",
            \"Fiction\",
            \"Fantasy\",
            \"Nestlé Smarties Book Prize winner\",
            \"Juvenile fiction\",
            \"Wizards\",
            \"Magic\",
            \"Schools\",
            \"Spanish language materials\",
            \"Magia\",
            \"Escuelas\",
            \"Ficción juvenil\",
            \"Novela fantástica\",
            \"Hogwarts School of Witchcraft and Wizardry (Imaginary place)\",
            \"Harry Potter (Fictitious character)\",
            \"Wizards -- Juvenile fiction\",
            \"Witches\",
            \"Hogwarts School of Witchcraft and Wizardry (Imaginary organization)\",
            \"Magos\",
            \"Translations from English\",
            \"Chinese fiction\",
            \"Orphans\",
            \"Aunts\",
            \"Uncles\",
            \"Cousins\",
            \"Determination (Personality trait) in children\",
            \"Friendship\",
            \"Potter, Harry (Fictitious character)\",
            \"Witches Fiction\",
            \"Wizards Fiction\",
            \"Schools Fiction\",
            \"England Fiction\",
            \"Magic -- Juvenile fiction\",
            \"Hogwarts School of Witchcraft and Wizardry (Imaginary place) -- Juvenile fiction\",
            \"Schools -- Juvenile fiction\",
            \"Wizards -- Fiction\",
            \"Magic -- Fiction\",
            \"Schools -- Fiction\",
            \"England -- Juvenile fiction\",
            \"England -- Fiction\",
            \"Fantasy & Magic\",
            \"Action & Adventure\",
            \"Witchcraft\",
            \"Harry Potter (Fictional character)\",
            \"Engels\",
            \"Social Themes\",
            \"Reading Level-Grade 11\",
            \"Reading Level-Grade 12\",
            \"Schools, fiction\",
            \"England, fiction\",
            \"Potter, harry (fictitious character), fiction\",
            \"Hogwarts school of witchcraft and wizardry (imaginary organization), fiction\",
            \"Wizards, fiction\",
            \"Magic, fiction\",
            \"Children\'s fiction\",
            \"Adventure and adventurers, fiction\",
            \"English literature\",
            \"Fiction, fantasy, general\",
            \"Large type books\",
            \"Hermione Granger (Fictitious character)\",
            \"Ron Weasley (Fictitious character)\",
            \"Latin language materials\",
            \"Children\'s stories\",
            \"Magiciens\",
            \"Romans, nouvelles, etc. pour la jeunesse\",
            \"Nécromancie\",
            \"Écoles\",
            \"Potter, Harry (Personnage fictif)\",
            \"Romans, nouvelles\",
            \"Magie\",
            \"Family\",
            \"Orphans & Foster Homes\",
            \"Magía\",
            \"Novela juvenil\",
            \"Juvenile\",
            \"Children\'s stories, English\",
            \"Sieg\",
            \"Basilisk\",
            \"Das Böse\",
            \"Das Gute\",
            \"Internat\",
            \"Lebensgefahr\",
            \"Lebensrettung\",
            \"List\",
            \"Magier\",
            \"Jugendbuch\",
            \"Kampf\",
            \"Schule\",
            \"Basilisk (Fabeltier)\",
            \"Junge\",
            \"Phönix\",
            \"Deutschland Grenzschutzkommando Mitte Schule\",
            \"Deutschland\",
            \"Friendship, fiction\",
            \"Hogwartes School of Witchcraft and Wizardry (Imaginary place)\",
            \"General\",
            \"Social Issues\",
            \"Witches, fiction\"
        ],
        \"subject_people\": [
            \"Harry Potter\",
            \"Hermione Granger\",
            \"Ron Weasley\",
            \"Albus Dumbledore\",
            \"Hagrid\",
            \"The Dursleys\",
            \"Gilderoy Lockhart\",
            \"Dobby\",
            \"Moaning Myrtle\",
            \"Ginny Weasley\",
            \"Draco Malfoy\",
            \"Hermine Granger\",
            \"Ron Weasly\",
            \"Harry Potter (Fictitious character)\"
        ],
        \"key\": \"/works/OL82537W\",
        \"authors\": [
            {
                \"author\": {
                    \"key\": \"/authors/OL23919A\"
                },
                \"type\": {
                    \"key\": \"/type/author_role\"
                }
            }
        ],
        \"excerpts\": [
            {
                \"excerpt\": \"Not for the first time, an argument had broken out over breakfast at number four, Privet Drive.\",
                \"comment\": \"first sentence\",
                \"author\": {
                    \"key\": \"/people/seabelis\"
                }
            }
        ],
        \"type\": {
            \"key\": \"/type/work\"
        },
        \"latest_revision\": 80,
        \"revision\": 80,
        \"created\": {
            \"type\": \"/type/datetime\",
            \"value\": \"2009-10-17T07:07:29.461716\"
        },
        \"last_modified\": {
            \"type\": \"/type/datetime\",
            \"value\": \"2022-06-22T07:57:49.863271\"
        }
    },
  • 请阅读minimal reproducible example。不要显示发出 Web 请求的代码,而是在代码中包含硬编码的字符串文字。确保其他人无需修改即可复制和粘贴并运行示例,并查看确切的问题。用代码的术语描述问题——当你运行代码并调用clean时,book_data会得到什么结果?您需要计算的确切结果是什么,以及这有什么关系你的代码已经计算了什么?或者-您是否尝试编写获取描述的代码版本?你遇到了什么问题?
  • 听起来好像 JSON 示例是顶层的字典列表,您想从每个字典中获取 \'description\' 键吗?好吧,问题很简单:显示的 dicts 有不同的键 - 不是所有的都包含 \'description\' 键。
  • Filter dict to contain only certain keys? 会回答你的问题吗?
  • 感谢本杰明提供的@KarlKnechtel 回答解决了我的问题
  • 好的,我不确定你想问的确切问题。如果就是这样,那么它应该是我链接的副本。

标签: python json list dictionary


【解决方案1】:

所有条目都没有titledescription 字段。因此,您必须使用try...except 子句来防止KeyErrors 发生。

def trim_json(d):
    newd = {}
    try:
        newd["title"] = d["title"]
    except KeyError:
        pass
    try:
        newd["description"] = d["description"]
    except KeyError:
        pass
    return newd

或者,以更优雅的方式,您可以在字典理解中使用过滤器:

 key_filter = ['title', 'description']
 cleaned_data = [{k:d[k] for k in key_filter if k in d} for d in book_data]

由于entries 列表中的第一个元素不是书籍数据(并且没有titledescription 键),您应该在第一个元素之后开始列表理解:

def clean():
    books = requests.get('https://openlibrary.org/authors/OL23919A/works.json')
    books_parsed = books.json()
    book_data = books_parsed['entries']
    cleaned_data = [trim_json(d) for d in book_data[1:]]
    return book_data

它可以防止获得与没有书相对应的空字典。

【讨论】:

    【解决方案2】:

    使用json 库。它默认安装在 python 中。

    假设您的 json 字符串存储在一个名为 json_str 的变量中,我们可以运行:

    import json
    
    info = json.loads(json_str)
    title = info['title']
    

    【讨论】:

    • 这不是关于如何解析 JSON 的问题。 OP 使用第三方 requests 库从 Internet 获取数据,该库解析 JSON 数据并下载它,代码如下:books_parsed = books.json()
    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多