从JSON中删除特殊字符

时间:2016-06-24 07:05:52

标签: python json pandas

我有一个包含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文件。

1 个答案:

答案 0 :(得分:0)

只处理 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)
相关问题