Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除json文件中的空字段_Python_Json_Pandas - Fatal编程技术网

Python 删除json文件中的空字段

Python 删除json文件中的空字段,python,json,pandas,Python,Json,Pandas,我有这个json文件,我必须删除一些空字段。我已经发布了我的json文件以及我期待的json文件。你能帮我写些什么代码吗 这是我的json文件: { "selection1": [ { "name": "Radisson Blu Azuri Resort & Spa", "url": "https://www.marideal.mu/hotel-deals/radisson-blu-azuri-resort-hotel-deals.html", "reviews"

我有这个json文件,我必须删除一些空字段。我已经发布了我的json文件以及我期待的json文件。你能帮我写些什么代码吗

这是我的json文件:

{
 "selection1": [
  {
   "name": "Radisson Blu Azuri Resort & Spa",
   "url": "https://www.marideal.mu/hotel-deals/radisson-blu-azuri-resort-hotel-deals.html",
   "reviews": [
{
     "name": "4",
     "date": "October 09, 2015",
     "review": "The personal was friendly , helpful n professional and put you at ease."
    },
    {
     "name": "3",
     "date": "July 20, 2015",
     "review": "Everyone really enjoy each moment we had there. The staffs are all very friendly and professionals."
    }
   ]
  },
  {
   "name": "Manta Cove by Horizon Holidays",
   "url": "https://www.marideal.mu/hotel-deals/manta-cove-by-horizon-holidays.html"
  },
  {
   "name": "Sous Le Badamier",
   "url": "https://www.marideal.mu/hotel-deals/sous-le-badamier.html"
  },
  {
   "name": "Honeymoon Special at Lagoon Attitude",
   "url": "https://www.marideal.mu/hotel-deals/honeymoon-special-at-lagoon-attitude.html",
   "reviews": [
    {
     "name": "3",
     "date": "January 20, 2020",
     "review": "Lagoon Attitude has been my best hotel stay ever in Mauritius. The eco friendly concept is one of a kind, esp. with the water fountains. My friends and I were warmly welcomed upon arrival. The staff were amazingly friendly, very professional and hospitable."
    },
{
     "name": "134",
     "date": "June 12, 2019",
     "review": "Had an amazing stay for 2 nights.\nThe cleanliness of the room is faultless"
    },
    {
     "name": "132",
     "date": "April 11, 2019",
     "review": "A highly recommendable hotel. Value for money and thanks to Marideal.\nThe room provided to us was very clean, and well maintained, we got a room in the 1800`s range, personal swimming pool along being cleaned everyday."
    },
    {
     "name": "131",
     "date": "January 24, 2019",
     "review": "Super week-end en famille. Merci à toute l'équipe du Radisson pour votre accueil, votre gentilesse et votre écoute."
    },
    {
     "name": "2",
     "date": "January 06, 2020",
     "review": "Our second visit to the hotel. Fantastic experience with the new Otentik concept. The personnel is the most welcoming and kind to us, namely Mr Kevin and Mr Jevissen, as well as Ms Veronique, to name a few."
    } 
   ]
  }
 ]
}
我应该如何删除空字段并使我的json文件看起来像:

{
 "selection1": [
  {
   "name": "Radisson Blu Azuri Resort & Spa",
   "url": "https://www.marideal.mu/hotel-deals/radisson-blu-azuri-resort-hotel-deals.html",
   "reviews": [
{
     "name": "4",
     "date": "October 09, 2015",
     "review": "The personal was friendly , helpful n professional and put you at ease."
    },
    {
     "name": "3",
     "date": "July 20, 2015",
     "review": "Everyone really enjoy each moment we had there. The staffs are all very friendly and professionals."
    }
   ]
  },
  {
   "name": "Honeymoon Special at Lagoon Attitude",
   "url": "https://www.marideal.mu/hotel-deals/honeymoon-special-at-lagoon-attitude.html",
   "reviews": [
    {
     "name": "3",
     "date": "January 20, 2020",
     "review": "Lagoon Attitude has been my best hotel stay ever in Mauritius. The eco friendly concept is one of a kind, esp. with the water fountains. My friends and I were warmly welcomed upon arrival. The staff were amazingly friendly, very professional and hospitable."
    },
{
     "name": "134",
     "date": "June 12, 2019",
     "review": "Had an amazing stay for 2 nights.\nThe cleanliness of the room is faultless"
    },
    {
     "name": "132",
     "date": "April 11, 2019",
     "review": "A highly recommendable hotel. Value for money and thanks to Marideal.\nThe room provided to us was very clean, and well maintained, we got a room in the 1800`s range, personal swimming pool along being cleaned everyday."
    },
    {
     "name": "131",
     "date": "January 24, 2019",
     "review": "Super week-end en famille. Merci à toute l'équipe du Radisson pour votre accueil, votre gentilesse et votre écoute."
    },
    {
     "name": "2",
     "date": "January 06, 2020",
     "review": "Our second visit to the hotel. Fantastic experience with the new Otentik concept. The personnel is the most welcoming and kind to us, namely Mr Kevin and Mr Jevissen, as well as Ms Veronique, to name a few."
    } 
   ]
  }
 ]
}

有人能给我解释一下如何删除空字段,以便我的json文件可以对每个循环进行“review”的预处理吗?

您可以使用
for
-loop来迭代
数据[“selection1”]
,并仅将具有
评论的元素复制到新字典中

import json

# read from file
#with open('input.json') as f:
#    data = json.load(f)

data = {
 "selection1": [
  {
   "name": "Radisson Blu Azuri Resort & Spa",
   "url": "https://www.marideal.mu/hotel-deals/radisson-blu-azuri-resort-hotel-deals.html",
   "reviews": [
{
     "name": "4",
     "date": "October 09, 2015",
     "review": "The personal was friendly , helpful n professional and put you at ease."
    },
    {
     "name": "3",
     "date": "July 20, 2015",
     "review": "Everyone really enjoy each moment we had there. The staffs are all very friendly and professionals."
    }
   ]
  },
  {
   "name": "Manta Cove by Horizon Holidays",
   "url": "https://www.marideal.mu/hotel-deals/manta-cove-by-horizon-holidays.html"
  },
  {
   "name": "Sous Le Badamier",
   "url": "https://www.marideal.mu/hotel-deals/sous-le-badamier.html"
  },
  {
   "name": "Honeymoon Special at Lagoon Attitude",
   "url": "https://www.marideal.mu/hotel-deals/honeymoon-special-at-lagoon-attitude.html",
   "reviews": [
    {
     "name": "3",
     "date": "January 20, 2020",
     "review": "Lagoon Attitude has been my best hotel stay ever in Mauritius. The eco friendly concept is one of a kind, esp. with the water fountains. My friends and I were warmly welcomed upon arrival. The staff were amazingly friendly, very professional and hospitable."
    },
    {
     "name": "134",
     "date": "June 12, 2019",
     "review": "Had an amazing stay for 2 nights.\nThe cleanliness of the room is faultless"
    },
    {
     "name": "132",
     "date": "April 11, 2019",
     "review": "A highly recommendable hotel. Value for money and thanks to Marideal.\nThe room provided to us was very clean, and well maintained, we got a room in the 1800`s range, personal swimming pool along being cleaned everyday."
    },
    {
     "name": "131",
     "date": "January 24, 2019",
     "review": "Super week-end en famille. Merci à toute l'équipe du Radisson pour votre accueil, votre gentilesse et votre écoute."
    },
    {
     "name": "2",
     "date": "January 06, 2020",
     "review": "Our second visit to the hotel. Fantastic experience with the new Otentik concept. The personnel is the most welcoming and kind to us, namely Mr Kevin and Mr Jevissen, as well as Ms Veronique, to name a few."
    } 
   ]
  }
 ]
}

# dictionary for element which you want to keep
new_data = {'selection1': []}

# copy item from old data to new data if it has 'reviews'
for item in data['selection1']:
    if 'reviews' in item:
        new_data['selection1'].append(item)
        #print(item['reviews'])
        #print('--')

# save in file
with open('output.json', 'w') as f:
    json.dump(new_data, f)

将JSON文件读入内存以创建字典,从字典中删除空元素,将字典保存回JSON文件我很难做到这一点,您能帮我吗?使用
for
-循环查看并复制到您想要保留的其他列表元素-然后将其分配到旧列表的位置您能帮我将其写在回答框中吗?具体问题是什么?你试过什么,做过什么研究吗?堆栈溢出不是免费的代码编写服务。见:。