1. <i id='DM895'><tr id='DM895'><dt id='DM895'><q id='DM895'><span id='DM895'><b id='DM895'><form id='DM895'><ins id='DM895'></ins><ul id='DM895'></ul><sub id='DM895'></sub></form><legend id='DM895'></legend><bdo id='DM895'><pre id='DM895'><center id='DM895'></center></pre></bdo></b><th id='DM895'></th></span></q></dt></tr></i><div id='DM895'><tfoot id='DM895'></tfoot><dl id='DM895'><fieldset id='DM895'></fieldset></dl></div>
    2. <legend id='DM895'><style id='DM895'><dir id='DM895'><q id='DM895'></q></dir></style></legend>

      <tfoot id='DM895'></tfoot>

        <small id='DM895'></small><noframes id='DM895'>

        • <bdo id='DM895'></bdo><ul id='DM895'></ul>

      1. 当尝试从JSON对象中删除值时,为什么我收到错误&#39;Unicode&#39;Object不支持项目

        Why do I get an error quot;#39;unicode#39; object does not support item deletionquot; when trying to delete values from a JSON object?(当尝试从JSON对象中删除值时,为什么我收到错误#39;Unicode#39;Object不支持项目删除Quot;?) - IT屋-程序员
          • <small id='ijUFj'></small><noframes id='ijUFj'>

          • <i id='ijUFj'><tr id='ijUFj'><dt id='ijUFj'><q id='ijUFj'><span id='ijUFj'><b id='ijUFj'><form id='ijUFj'><ins id='ijUFj'></ins><ul id='ijUFj'></ul><sub id='ijUFj'></sub></form><legend id='ijUFj'></legend><bdo id='ijUFj'><pre id='ijUFj'><center id='ijUFj'></center></pre></bdo></b><th id='ijUFj'></th></span></q></dt></tr></i><div id='ijUFj'><tfoot id='ijUFj'></tfoot><dl id='ijUFj'><fieldset id='ijUFj'></fieldset></dl></div>
            <tfoot id='ijUFj'></tfoot>

                <tbody id='ijUFj'></tbody>

              <legend id='ijUFj'><style id='ijUFj'><dir id='ijUFj'><q id='ijUFj'></q></dir></style></legend>
                • <bdo id='ijUFj'></bdo><ul id='ijUFj'></ul>
                  本文介绍了当尝试从JSON对象中删除值时,为什么我收到错误&#39;Unicode&#39;Object不支持项目删除&Quot;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图遍历对象列表,从每个对象中删除一个元素。每个对象都是新的一行。然后,我尝试按原样保存新文件,而不保存对象中包含的元素。

                  {
                      "business_id": "fNGIbpazjTRdXgwRY_NIXA",
                      "full_address": "1201 Washington Ave
                  Carnegie, PA 15106",
                      "hours": {
                          "Monday": {
                              "close": "23:00",
                              "open": "11:00"
                          },
                          "Tuesday": {
                              "close": "23:00",
                              "open": "11:00"
                          },
                          "Friday": {
                              "close": "23:00",
                              "open": "11:00"
                          },
                          "Wednesday": {
                              "close": "23:00",
                              "open": "11:00"
                          },
                          "Thursday": {
                              "close": "23:00",
                              "open": "11:00"
                          },
                          "Saturday": {
                              "close": "23:00",
                              "open": "11:00"
                          }
                      },
                      "open": true,
                      "categories": ["Bars", "American (Traditional)", "Nightlife", "Lounges", "Restaurants"],
                      "city": "Carnegie",
                      "review_count": 7,
                      "name": "Rocky's Lounge",
                      "neighborhoods": [],
                      "longitude": -80.0849416,
                      "state": "PA",
                      "stars": 4.0,
                      "latitude": 40.3964688,
                      "attributes": {
                          "Alcohol": "full_bar",
                          "Noise Level": "average",
                          "Music": {
                              "dj": false
                          },
                          "Attire": "casual",
                          "Ambience": {
                              "romantic": false,
                              "intimate": false,
                              "touristy": false,
                              "hipster": false,
                              "divey": false,
                              "classy": false,
                              "trendy": false,
                              "upscale": false,
                              "casual": false
                          },
                          "Good for Kids": true,
                          "Wheelchair Accessible": true,
                          "Good For Dancing": false,
                          "Delivery": false,
                          "Dogs Allowed": false,
                          "Coat Check": false,
                          "Smoking": "no",
                          "Accepts Credit Cards": true,
                          "Take-out": true,
                          "Price Range": 1,
                          "Outdoor Seating": false,
                          "Takes Reservations": false,
                          "Waiter Service": true,
                          "Wi-Fi": "free",
                          "Caters": false,
                          "Good For": {
                              "dessert": false,
                              "latenight": false,
                              "lunch": false,
                              "dinner": false,
                              "brunch": false,
                              "breakfast": false
                          },
                          "Parking": {
                              "garage": false,
                              "street": false,
                              "validated": false,
                              "lot": true,
                              "valet": false
                          },
                          "Has TV": true,
                          "Good For Groups": true
                      },
                      "type": "business"
                  }
                  

                  我需要删除小时元素中包含的信息,但是这些信息并不总是相同的。有些包含所有日期,有些仅包含一天或两天信息。

                  这是我尝试过的代码:

                  import json
                  
                  with open('data.json') as data_file:
                      data = json.load(data_file)
                      for element in data: 
                          del element['hours']
                  

                  但是,运行代码时出现错误:

                  TypeError:‘Unicode’对象不支持项删除

                  推荐答案

                  假设您要覆盖同一文件:

                  import json
                  
                  with open('data.json', 'r') as data_file:
                      data = json.load(data_file)
                  
                  for element in data:
                      element.pop('hours', None)
                  
                  with open('data.json', 'w') as data_file:
                      data = json.dump(data, data_file)
                  

                  dict.pop(<key>, not_found=None)可能就是您要找的,如果我理解您的要求的话。因为它将删除hours项(如果存在),如果不存在则不会失败。

                  不过,我不确定我是否理解为什么小时键是否包含几天对您有影响,因为您只是想去掉整个键/值对,对吗?

                  现在,如果您确实希望使用del而不是pop,下面是如何使您的代码工作的方法:

                  import json
                  
                  with open('data.json') as data_file:
                      data = json.load(data_file)
                  
                  for element in data:
                      if 'hours' in element:
                          del element['hours']
                  
                  with open('data.json', 'w') as data_file:
                      data = json.dump(data, data_file)
                  

                  如果要将其写入其他文件,只需更改第二个open语句中的文件名。

                  您可能已经注意到,我必须更改缩进,以便在数据清理阶段关闭该文件,并且可以在结束时覆盖该文件。

                  with是所谓的上下文管理器,无论它提供什么(这里是data_file文件描述符),在该上下文中可用。这意味着with挡路的缩进一结束,文件就会关闭,上下文以及变为无效/过时的文件描述符也会结束。

                  如果不这样做,您将无法在写入模式下打开文件并获取要写入的新文件描述符。

                  这篇关于当尝试从JSON对象中删除值时,为什么我收到错误&#39;Unicode&#39;Object不支持项目删除&Quot;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                    <tbody id='TQeEf'></tbody>
                  <tfoot id='TQeEf'></tfoot>
                • <small id='TQeEf'></small><noframes id='TQeEf'>

                  1. <legend id='TQeEf'><style id='TQeEf'><dir id='TQeEf'><q id='TQeEf'></q></dir></style></legend>

                          <bdo id='TQeEf'></bdo><ul id='TQeEf'></ul>

                          • <i id='TQeEf'><tr id='TQeEf'><dt id='TQeEf'><q id='TQeEf'><span id='TQeEf'><b id='TQeEf'><form id='TQeEf'><ins id='TQeEf'></ins><ul id='TQeEf'></ul><sub id='TQeEf'></sub></form><legend id='TQeEf'></legend><bdo id='TQeEf'><pre id='TQeEf'><center id='TQeEf'></center></pre></bdo></b><th id='TQeEf'></th></span></q></dt></tr></i><div id='TQeEf'><tfoot id='TQeEf'></tfoot><dl id='TQeEf'><fieldset id='TQeEf'></fieldset></dl></div>