删除JSON对象中的元素

我试图遍历对象列表,从每个对象中删除一个元素。每个对象都是换行符。我试图然后按原样保存新文件,而对象中不包含任何元素。我知道这可能是一个简单的任务,但似乎无法完成这项工作。如果有人可以伸出援手,将不胜感激。谢谢。

{

"business_id": "fNGIbpazjTRdXgwRY_NIXA",

"full_address": "1201 Washington Ave\nCarnegie, 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"

}

我需要删除小时元素中包含的信息,但是信息并不总是相同的。有些包含全天,有些仅包含一两天的信息。我尝试使用的代码是Pyton,我整天都在搜索该代码以解决问题。我对Python不太熟练。任何帮助,将不胜感激。

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块的缩进结束,文件就会关闭并且上下文也将结束,文件描述符也变得无效/过时。

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

我希望已经足够清楚了…

这次,您似乎显然需要执行以下操作:

with open('dest_file.json', 'w') as dest_file:

with open('source_file.json', 'r') as source_file:

for line in source_file:

element = json.loads(line.strip())

if 'hours' in element:

del element['hours']

dest_file.write(json.dumps(element))

以上是 删除JSON对象中的元素 的全部内容, 来源链接: utcz.com/qa/431518.html

回到顶部