我想从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文件的正确格式。你知道吗
好吧-既然您原来的JSON文件可能不包含
newline characters
,我希望这是可行的,甚至可能更准确编辑-使用上面提供的字符串在下面给出您的评论。希望我能理解你的要求。我使用我提供的两个regex示例。
是的。你知道吗
或尝试此功能:
对于调用函数的行,将
'PATH_TO_YOUR_FILE'
替换为。。。你知道,文件的路径…相关问题 更多 >
编程相关推荐