从 JSON 中删除特殊字符
Posted
技术标签:
【中文标题】从 JSON 中删除特殊字符【英文标题】:Removing special characters from JSON 【发布时间】:2016-10-26 17:25:14 【问题描述】:我有一个包含 3 个对象的 JSON 文件,如下所示:
["ID": "44585", "TITLE": "A day in *Australia*=/", "MailThread": ["email": " Its a great day, isn't it.", "email": " Yes, indeed.", "email": " Great Day!!!"], "SUMMARY": "", "NAME": "Mike's Journal", "PRIORITY": 3, "FLIGHTTICKETNO": 25565826, "CODENAME": "ausTrip", "DESC": "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!","ID": "44585", "TITLE": "A day in *Australia*=/", "MailThread": ["email": " Its a great day, isn't it.", "email": " Yes, indeed.", "email": " Great Day!!!"], "SUMMARY": "", "NAME": "Mike's Journal", "PRIORITY": 3, "FLIGHTTICKETNO": 25565826, "CODENAME": "ausTrip", "DESC": "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!","ID": "44585", "TITLE": "A day in *Australia*=/", "MailThread": ["email": " Its a great day, isn't it.", "email": " Yes, indeed.", "email": " Great Day!!!"], "SUMMARY": "", "NAME": "Mike's Journal", "PRIORITY": 3, "FLIGHTTICKETNO": 25565826, "CODENAME": "ausTrip", "DESC": "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!"]
我需要从 TITLE 中删除 *=/、&^#@ 等特殊字符,从所有 3 个对象中删除 MailThread 和 DESC 中的多个电子邮件,并将编辑后的内容写入新的 json 文件。
【问题讨论】:
输出是数据框?还是json?您可以添加所需的输出吗? 输出是 json..刚刚编辑了查询..请再看一遍!! “我需要牛排”不是问题。 这就是我想要做的import pandas as pd df = pd.read_json('JsonData') jsonobj = df['TITLE'] for i in jsonobj: jsonobj[i].replace('[','') print(jsonobj)
它不起作用,也许这不是要牛排而是要帮忙!
您说您需要删除MailThread 中的多封电子邮件。你的意思是全部删除还是选中?明确说明,如果您提供准确的输出会很好。
【参考方案1】:
只处理TITLE,如需其他请自行处理如下:
$ python demo.py
'PRIORITY': 3, 'TITLE': 'A day in Australia', 'FLIGHTTICKETNO': 25565826, 'MailThread': ['email': " Its a great day, isn't it.", 'email': ' Yes, indeed.', 'email': ' Great Day!!!'], 'CODENAME': 'ausTrip', 'SUMMARY': '', 'NAME': "Mike's Journal", 'ID': '44585', 'DESC': "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!"
'PRIORITY': 3, 'TITLE': 'A day in Australia', 'FLIGHTTICKETNO': 25565826, 'MailThread': ['email': " Its a great day, isn't it.", 'email': ' Yes, indeed.', 'email': ' Great Day!!!'], 'CODENAME': 'ausTrip', 'SUMMARY': '', 'NAME': "Mike's Journal", 'ID': '44585', 'DESC': "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!"
'PRIORITY': 3, 'TITLE': 'A day in Australia', 'FLIGHTTICKETNO': 25565826, 'MailThread': ['email': " Its a great day, isn't it.", 'email': ' Yes, indeed.', 'email': ' Great Day!!!'], 'CODENAME': 'ausTrip', 'SUMMARY': '', 'NAME': "Mike's Journal", 'ID': '44585', 'DESC': "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!"
代码
#!/usr/bin/python
def func(title):
for ch in "*=/,&^#@":
title = title.replace(ch, '')
return title
if __name__ == "__main__":
data = ['CODENAME': 'ausTrip',
'DESC': "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!",
'FLIGHTTICKETNO': 25565826,
'ID': '44585',
'MailThread': ['email': " Its a great day, isn't it.",
'email': ' Yes, indeed.',
'email': ' Great Day!!!'],
'NAME': "Mike's Journal",
'PRIORITY': 3,
'SUMMARY': '',
'TITLE': 'A day in *Australia*=/',
'CODENAME': 'ausTrip',
'DESC': "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!",
'FLIGHTTICKETNO': 25565826,
'ID': '44585',
'MailThread': ['email': " Its a great day, isn't it.",
'email': ' Yes, indeed.',
'email': ' Great Day!!!'],
'NAME': "Mike's Journal",
'PRIORITY': 3,
'SUMMARY': '',
'TITLE': 'A day in *Australia*=/',
'CODENAME': 'ausTrip',
'DESC': "I am really feeling great*&^% to be here!! I love this place, and find it's places amazin#@$. Thanks!!",
'FLIGHTTICKETNO': 25565826,
'ID': '44585',
'MailThread': ['email': " Its a great day, isn't it.",
'email': ' Yes, indeed.',
'email': ' Great Day!!!'],
'NAME': "Mike's Journal",
'PRIORITY': 3,
'SUMMARY': '',
'TITLE': 'A day in *Australia*=/']
for item in data:
if item and 'TITLE' in item.keys():
title = func(item['TITLE'])
item['TITLE'] = title
print(item)
【讨论】:
以上是关于从 JSON 中删除特殊字符的主要内容,如果未能解决你的问题,请参考以下文章