简体   繁体   English

如何使用python过滤json数据

[英]How to filter json data using python

i am trying to understand how to filter json data using python my json looks like this :我想了解如何使用 python 过滤 json 数据,我的 json 如下所示:

[
    {
        "comments_full": []
    },
    {
        "comments_full": [
            {
                "comment_id": "433934735000014",
                "comment_url": "https:\\/\\/facebook.com\\/433934735000014",
                "commenter_id": "100002886314120",
                "commenter_url": "https:\\/\\/facebook.com\\/loubnaharifi?fref=nf&rc=p&refid=52&__tn__=R",
                "commenter_name": "Loubna Harifi",
                "commenter_meta": null,
                "comment_text": "\\u00c0 18h \\u00e7a commence",
                "comment_time": 1636502400000,
                "comment_image": null,
                "comment_reactors": [
                    {
                        "name": "Bouygues Telecom",
                        "link": "https:\\/\\/facebook.com\\/bouyguestelecom\\/?fref=pb",
                        "type": "like"
                    }
                ],
                "comment_reactions": {
                    "like": 55,
                    "love": 12,
                    "haha": 4,
                    "wow": 1,
                    "sad": 1,
                    "angry": 4
                },
                "comment_reaction_count": 77,
                "replies": [
                    {
                        "comment_id": "433935588333262",
                        "comment_url": "https:\\/\\/facebook.com\\/433935588333262",
                        "commenter_id": "94533530492",
                        "commenter_url": "https:\\/\\/facebook.com\\/bouyguestelecom\\/?rc=p&refid=52&__tn__=%7ERR",
                        "commenter_name": "Bouygues Telecom",
                        "commenter_meta": null,
                        "comment_text": "Oui tout \\u00e0 fait ! RDV \\u00e0 18h \\ud83d\\ude42",
                        "comment_time": 1636502400000,
                        "comment_image": null,
                        "comment_reactors": [
                            {
                                "name": "Maryline Moss",
                                "link": "https:\\/\\/facebook.com\\/mary.poilue.92?fref=pb",
                                "type": "like"
                            },
                            {
                                "name": "Jess Robic",
                                "link": "https:\\/\\/facebook.com\\/JessicaRbc91?fref=pb",
                                "type": "like"
                            }
                        ],
                        "comment_reactions": {
                            "like": 55,
                            "love": 12,
                            "haha": 4,
                            "wow": 1,
                            "sad": 1,
                            "angry": 4
                        },
                        "comment_reaction_count": 77
                         ...

what im trying to extract is我试图提取的是

  • comment_id评论 ID
  • commenter_name评论者姓名
  • comment_text评论文本

i searched i lot about this but i had no luck finding anything useful我搜索了很多关于这个但我没有找到任何有用的东西

here is waht i tried so far :这是我到目前为止尝试过的:

df_ori[["comments_full"]].to_excel(r'C:/Users/stefa/OneDrive/Bureau/Scrap website/Last test/Scrapped_FB.xlsx', index = None, header=True)

cSvFilePath = "C:/Users/stefa/OneDrive/Bureau/Scrap website/Last test/Scrapped_FB.csv"
jsonFilePath = "C:/Users/stefa/OneDrive/Bureau/Scrap website/Last test/Scrapped_FB.json"
# Read the CSV and add the data to a diction
data = {}
with open(cSvFilePath, encoding="cp437", errors='ignore') as csvFile:
   csvReader = csv.DictReader(csvFile)
  for csvRow in csvReader:
    hmid = csvRow["comment_text"]
    data[hmid] = csvRow

file = dataframe(data, columns= ['comments_full'])
file.to_json(r'C:/Users/stefa/OneDrive/Bureau/Scrap website/Last test/Scrapped_FB.json',orient='split')

i just started learning python and json tables and object wasn't something that i worked with a lot我刚开始学习 python 和 json 表,对象不是我经常使用的东西

i hope someone can guide me to the right direction我希望有人能引导我走向正确的方向

thank you谢谢你

you should use the json module :你应该使用 json 模块:

import json

jsonObj = json.load(YOUR FILE)

print(jsonObj["FIELD_YOU_WHANT"])

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

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