【问题标题】:Convert Json to Csv with given headers使用给定的标头将 Json 转换为 Csv
【发布时间】:2021-06-16 07:40:25
【问题描述】:

我在处理 json 文件方面相对较新。我有一个 json 数据集,我想将其转换为带有相关标题的 CSV-

Headers = ['Transcripts', 'Food', 'Area', 'PriceRange'] 

数据格式如下:

[
    {
        "dial": [
            {
                "turn": 0, 
                "usr": {
                    "transcript": "I need to find an expensive restauant that's in the south section of the city.", 
                    "slu": [
                        {
                            "act": "inform", 
                            "slots": [
                                [
                                    "pricerange", 
                                    "expensive"
                                ]
                            ]
                        }, 
                        {
                            "act": "inform", 
                            "slots": [
                                [
                                    "area", 
                                    "south"
                                ]
                            ]
                        }
                    ]
                }, 
                "sys": {
                    "sent": "There are several restaurants in the south part of town that serve expensive food. Do you have a cuisine preference?", 
                    "DA": [
                        "food"
                    ]
                }
            }, 
            {
                "turn": 1, 
                "usr": {
                    "transcript": "No I don't care about the type of cuisine.", 
                    "slu": [
                        {
                            "act": "inform", 
                            "slots": [
                                [
                                    "pricerange", 
                                    "expensive"
                                ]
                            ]
                        }, 
                        {
                            "act": "inform", 
                            "slots": [
                                [
                                    "food", 
                                    "dontcare"
                                ]
                            ]
                        }, 
                        {
                            "act": "inform", 
                            "slots": [
                                [
                                    "area", 
                                    "south"
                                ]
                            ]
                        }
                    ]
                }, 
                "sys": {
                    "sent": "Chiquito Restaurant Bar is a Mexican restaurant located in the south part of town.", 
                    "DA": []
                }
            }, 
            {
                "turn": 2, 
                "usr": {
                    "transcript": "What is their address?", 
                    "slu": [
                        {
                            "act": "request", 
                            "slots": [
                                [
                                    "slot", 
                                    "address"
                                ]
                            ]
                        }, 
                        {
                            "act": "inform", 
                            "slots": [
                                [
                                    "pricerange", 
                                    "expensive"
                                ]
                            ]
                        }, 
                        {
                            "act": "inform", 
                            "slots": [
                                [
                                    "area", 
                                    "south"
                                ]
                            ]
                        }, 
                        {
                            "act": "inform", 
                            "slots": [
                                [
                                    "food", 
                                    "dontcare"
                                ]
                            ]
                        }
                    ]
                }, 
                "sys": {
                    "sent": "There address is 2G Cambridge Leisure Park Cherry Hinton Road Cherry Hinton, it there anything else I can help you with?", 
                    "DA": [
                    ]
                }
            }, 
          

我的目标是像这样隔离它,如果 act = 'inform' 然后获取成绩单、区域、食物、价格范围。

我尝试将其转换为 CSV,但它存储了价格范围和昂贵等字符串。我希望价格范围作为我的数据的标题和昂贵,这是转换后的 csv 示例 -

【问题讨论】:

  • 到目前为止你尝试过什么?向我们展示您的代码!
  • 我使用了这个网络工具 - convertcsv.com/json-to-csv.htm
  • edit您的问题包含您尝试过的任何代码,并显示您对该 JSON 的预期输出
  • 您发布的JSON无效,最后缺少]}]

标签: python json csv


【解决方案1】:
import csv
import io

json_data = [{"dial": [
    {"turn": 0,
       "usr": {
           "transcript": "I need to find an expensive restauant that's in the south section of the city.",
           "slu": [
               {"act": "inform", "slots": [["pricerange", "expensive"]]},
               {"act": "inform", "slots": [["area", "south"]]}
           ]
       },
       "sys": {
           "sent": "There are several restaurants in the south part of town that serve expensive food. Do you have a cuisine preference?",
           "DA": ["food"]
       }
    },
    {"turn": 1,
       "usr": {
           "transcript": "No I don't care about the type of cuisine.",
           "slu": [
               {"act": "inform", "slots": [["pricerange", "expensive"]]},
               {"act": "inform", "slots": [["food", "dontcare"]]},
               {"act": "inform", "slots": [["area", "south"]]}
           ]
       },
       "sys": {
           "sent": "Chiquito Restaurant Bar is a Mexican restaurant located in the south part of town.",
           "DA": []
       }
    },
    {"turn": 2,
       "usr": {
           "transcript": "What is their address?",
           "slu": [
               {"act": "request", "slots": [["slot", "address"]]},
               {"act": "inform", "slots": [["pricerange", "expensive"]]},
               {"act": "inform", "slots": [["area", "south"]]},
               {"act": "inform", "slots": [["food", "dontcare"]]}
           ]
       },
       "sys": {
           "sent": "There address is 2G Cambridge Leisure Park Cherry Hinton Road Cherry Hinton, it there anything else I can help you with?",
           "DA": []
       }
    }
]}]
Headers = ['Transcripts', 'Food', 'Area', 'PriceRange']

fake_file = io.StringIO()
writer = csv.writer(fake_file)
writer.writerow(["Turn"] + Headers)

for turn_object in json_data[0]["dial"]:
    turn_number = turn_object["turn"]
    transcript = turn_object["usr"]["transcript"]
    area, food, price_range = "-", "-", "-"  # (re-)initialization
    for act_object in turn_object["usr"]["slu"]:
        if act_object["act"] == "inform":
            if act_object["slots"][0][0] == "pricerange":
                price_range = act_object["slots"][0][1]
            elif act_object["slots"][0][0] == "area":
                area = act_object["slots"][0][1]
            elif act_object["slots"][0][0] == "food":
                food = act_object["slots"][0][1]
    writer.writerow([turn_number, transcript, food, area, price_range])

print(fake_file.getvalue())
# output (edited for readability) :
# Turn,Transcripts                                                                   ,Food    ,Area ,PriceRange
# 0   ,I need to find an expensive restauant that's in the south section of the city.,-       ,south,expensive
# 1   ,No I don't care about the type of cuisine.                                    ,dontcare,south,expensive
# 2   ,What is their address?                                                        ,dontcare,south,expensive

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 2019-08-21
    • 1970-01-01
    • 2015-08-23
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多