拆分re.匹配对象类型单词

发布于 2025-02-03 10:27:16 字数 803 浏览 1 评论 0原文

我有此代码扫描我的电子邮件并返回一个重要的识别号码及其相应日期。我正在尝试将数字和日期分为由列分开的单独子字符串(最终计划是将所有信息都输入CSV),但是我会收到以下错误:attributeError:'re..match'对象没有属性'分裂'。任何帮助都将受到赞赏。这是我的代码:

pattern = re.compile(r'[a-zA-Z]+[0-9]+ [0-9]+/[0-9]+/[0-9]+')
matches = pattern.finditer(body)
for match in matches:
    matches.split()

我希望输出看起来如下:

AAA111111, 1/1/2022
BBB222222, 1/1/2022

等等。目标是将其变成我可以在其他地方导入的CSV

,这是进入身体的原因:

'' 感谢您的跟进。这是您订单的更新。

PUU128377 5/22/2023
PUN102938 11/1/2024
PUU012938 10/01/2025

提出任何其他问题 ''

带有扩展信息的新电子邮件

PUU128377 Line 20 Seq 1 5/22/2023
PUN102938 Line 100 Seq 8 11/1/2024
PUU012938 Line 120 Seq 4 1/1/2025

I have this code that scans my emails and returns an important identifying number and its corresponding date. I am trying to split the number and the date into separate substrings separated by columns (ultimate plan is to get them all the info into a csv), but I get the following error: AttributeError: 're.Match' object has no attribute 'split'. Any help is appreciated. Here's my code:

pattern = re.compile(r'[a-zA-Z]+[0-9]+ [0-9]+/[0-9]+/[0-9]+')
matches = pattern.finditer(body)
for match in matches:
    matches.split()

I expect the output to look like the following:

AAA111111, 1/1/2022
BBB222222, 1/1/2022

and so on. Goal is to turn it into a csv that I can import elsewhere

Also, here is what goes into body:

''
Thanks for following up. Here’s an update on your orders.

PUU128377 5/22/2023
PUN102938 11/1/2024
PUU012938 10/01/2025

Reach out with any further questions
''

New email with extended info

PUU128377 Line 20 Seq 1 5/22/2023
PUN102938 Line 100 Seq 8 11/1/2024
PUU012938 Line 120 Seq 4 1/1/2025

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2025-02-10 10:27:16

尝试:

import re
import csv

body = """\
PUU128377 Line 20 Seq 1 5/22/2023
PUN102938 Line 100 Seq 8 11/1/2024
PUU012938 Line 120 Seq 4 1/1/2025
"""

pattern = re.compile(
    r"([a-zA-Z]+[0-9]+) Line ([0-9]+) Seq ([0-9]) ([0-9]+/[0-9]+/[0-9]+)"
)
matches = pattern.finditer(body)

with open("data.csv", "w") as f_out:
    writer = csv.writer(f_out)
    writer.writerows(map(lambda m: m.groups(), matches))

这将创建data.csv文件:

PUU128377,20,1,5/22/2023
PUN102938,100,8,11/1/2024
PUU012938,120,4,1/1/2025

编辑:带有新输入的更新答案

Try:

import re
import csv

body = """\
PUU128377 Line 20 Seq 1 5/22/2023
PUN102938 Line 100 Seq 8 11/1/2024
PUU012938 Line 120 Seq 4 1/1/2025
"""

pattern = re.compile(
    r"([a-zA-Z]+[0-9]+) Line ([0-9]+) Seq ([0-9]) ([0-9]+/[0-9]+/[0-9]+)"
)
matches = pattern.finditer(body)

with open("data.csv", "w") as f_out:
    writer = csv.writer(f_out)
    writer.writerows(map(lambda m: m.groups(), matches))

This creates data.csv file:

PUU128377,20,1,5/22/2023
PUN102938,100,8,11/1/2024
PUU012938,120,4,1/1/2025

Edit: Updated answer with new input

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文