如何从json文件pythonically中提取电子邮件的主题?

2024-04-26 10:51:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从json文件中提取垃圾邮件的主题,但主题可以在文件中的任何位置,在“content”或“header”或“body”中。使用regex,我无法提取主题,尽管使用下面的这个代码:有人能帮我吗指出下面的正则表达式或代码中有什么不正确?你知道吗

import re
import json
with open("test.json", 'r') as fp:
    json_decode = json.loads(fp.read())


p = re.compile('([\[\(] *)?.*(RE?S?|FWD?|re\[\d+\]?) *([-:;)\]][ :;\])-]*|$)|\]+ *$', re.IGNORECASE)
for line in json_decode:
    print(p.sub('', line).strip())

输出(不正确):车身

我的测试.json文件如下:

    {'attachment': [{'content_header': {'content-disposition': ['attachment; '
                                                        'filename="image006.jpg"'],
                                'content-id': ['<image006.jpg@01D35D21.756FEE10>']
     'body': [{'content': ' \n'
                  ' \n'
                  'From: eCard Delivery [mailto:ecards@789greeting.com] \n'
                  'Sent: Monday, November 13, 2017 9:14 AM\n'
                  'To: Zhang, Jerry (352A-Affiliate) '

                  'Subject: Warmest Wishes! You have a Happy Thanksgiving '
                  'ecard delivery!\n'
                  ' \n'
                  ' \tDear Jerry,\n'
     'header': {'date': '2017-11-14T08:20:42-08:00',

        'header': {'accept-language': ['en-US'],
                   'content-language': ['en-US'],
                   'content-type': ['multipart/mixed; '
                                    'boundary="--boundary-LibPST-iamunique-1500317751_-_-"'],
                   'date': ['Tue, 14 Nov 2017 08:20:42 -0800']
                   'subject': 'FW: Warmest Wishes! You have a Happy Thanksgiving '
                   'ecard delivery!'}}

^以上是json文件的正确格式。你知道吗


Tags: 文件代码importrejson主题attachmentline
1条回答
网友
1楼 · 发布于 2024-04-26 10:51:59

好吧-既然您原来的JSON文件可能不包含newline characters,我希望这是可行的,甚至可能更准确

>>> string = '''{'attachment': [{'content_header': {'content-disposition': ['attachment; ''filename="image006.jpg"'],'content-id': ['<image006.jpg@01D35D21.756FEE10>'] 'body': [{'content': ' '' ''From: eCard Delivery [mailto:ecards@789greeting.com] ''Sent: Monday, November 13, 2017 9:14 AM''To: Zhang, Jerry (352A-Affiliate) ''Subject: Warmest Wishes! You have a Happy Thanksgiving ''ecard delivery!'' ''   Dear Jerry,' 'header': {'date': '2017-11-14T08:20:42-08:00','header': {'accept-language': ['en-US'], 'content-language': ['en-US'], 'content-type': ['multipart/mixed; ''boundary=" boundary-LibPST-iamunique-1500317751_-_-"'], 'date': ['Tue, 14 Nov 2017 08:20:42 -0800'] 'subject': 'FW: Warmest Wishes! You have a Happy Thanksgiving ' 'ecard delivery!'}}'''

>>> subjects_test = re.findall('([\'|\"]*[\S]ubject[\S\s]+?[\'|\"]+)(?=\n|$|\s|\})', string)


>>> for subject in subjects_test:
        print(subject)



#OUPUT: #Kind of off I guess, but I don't know the full format of the file so this is the safest bet    

''Subject: Warmest Wishes! You have a Happy Thanksgiving ''ecard delivery!''
'subject': 'FW: Warmest Wishes! You have a Happy Thanksgiving '

编辑-使用上面提供的字符串在下面给出您的评论。希望我能理解你的要求。我使用我提供的两个regex示例。

>>> string = '''{'attachment': [{'content_header': {'content-disposition': ['attachment; '
                                                    'filename="image006.jpg"'],
                            'content-id': ['<image006.jpg@01D35D21.756FEE10>']
 'body': [{'content': ' \n'
              ' \n'
              'From: eCard Delivery [mailto:ecards@789greeting.com] \n'
              'Sent: Monday, November 13, 2017 9:14 AM\n'
              'To: Zhang, Jerry (352A-Affiliate) '

              'Subject: Warmest Wishes! You have a Happy Thanksgiving '
              'ecard delivery!\n'
              ' \n'
              ' \tDear Jerry,\n'
 'header': {'date': '2017-11-14T08:20:42-08:00',

    'header': {'accept-language': ['en-US'],
               'content-language': ['en-US'],
               'content-type': ['multipart/mixed; '
                                'boundary=" boundary-LibPST-iamunique-1500317751_-_-"'],
               'date': ['Tue, 14 Nov 2017 08:20:42 -0800']
               'subject': 'FW: Warmest Wishes! You have a Happy Thanksgiving '
               'ecard delivery!'}}'''



>>> subjects_test_1 = re.findall('([\'\"]*[S|s]ubject[:\s]*?(?:[\'|\"]*[\S\s]*?(?=[\'|\"])*))(?=\n|$)', string)


>>> for subject in subjects_test_1:
        print(subject)

#OUPUT: 
'Subject: Warmest Wishes! You have a Happy Thanksgiving '
'subject': 'FW: Warmest Wishes! You have a Happy Thanksgiving '


########################################################

>>> subjects_test_2 = re.findall('([\'|\"]*[\S]ubject[\S\s]+?[\'|\"]*)(?=\n|$)', string)


>>> for subject in subjects_test_2:
        print(subject)

#OUPUT: 
'Subject: Warmest Wishes! You have a Happy Thanksgiving '
'subject': 'FW: Warmest Wishes! You have a Happy Thanksgiving '

是的。你知道吗

或尝试此功能:

对于调用函数的行,将'PATH_TO_YOUR_FILE'替换为。。。你知道,文件的路径…

>>> def email_subject_parse(file_path):
        import re
        email_subjects = []
        try:
            with open(file_path) as file:
                string = file.read()
                email_subjects = re.findall('([\'\"]*[S|s]ubject[:\s]*?(?:[\'|\"]*[\S\s]*?(?=[\'|\"])*))(?=\n|$)', string)
                #Or less complicated 
                #email_subjects = re.findall('([\'|\"]*[\S]ubject[\S\s]+?[\'|\"]*)(?=\n|$)', string)
                return email_subjects
        except:
            print('You have likely provided a bad file path')


>>> subjects = email_subject_parse('PATH_TO_YOUR_FILE')


>>> for subject in subjects:
        print(subject)



#OUPUT: 
'Subject: Warmest Wishes! You have a Happy Thanksgiving '
'subject': 'FW: Warmest Wishes! You have a Happy Thanksgiving '

相关问题 更多 >