gpt4 book ai didi

用于模式研究的 Python 正则表达式

转载 作者:行者123 更新时间:2023-12-01 08:47:13 26 4
gpt4 key购买 nike

我想从文本数据中提取关键字和句子之间的行。这是我的数据,

CUSTOMER SUPPLIED DATA: 
- topic: Sign in & Password Support
- First Name: Brenda
- Last Name: Delacruz
- Account number: xxxxxxxxx
- U-verse 4-digit PIN: My PIN is
- 4 digit PIN: xxxx
- Email: deedelacruz28806@yahoo.com
- I need help with: Forgot password or ID

*** System::[chat.automatonClientOutcome] Hello! How may I help you today? *** System::[chat.queueWaitDisplayed] We are currently experiencing very high chat volumes which may cause long delays. An agent will be with you as soon as possible.

这里帮助我提取 * 系统行开始之前关键字“CUSTOMER SUPPLIED DATA:”下的行。 (提取 CUSTOMER SUPPLIED DATA: 和 * 系统行之间的行)。

我尝试过以下代码,

m = re.search('CUSTOMER SUPPLIED DATA:\s*([^\n]+)', dt["chat_consolidation" 
[546])

m.group(1)

这在 CUSTOMER SUPPLIED DATA: 和 *** 系统行之间只给出了一行

输出如下:

[out]: - topic: Sign in & Password Support

但是我需要的输出应该是这样的,

[Out]: - topic: Sign in & Password Support
- First Name: Brenda
- Last Name: Delacruz
- Account number: xxxxxxxxx
- U-verse 4-digit PIN: My PIN is
- 4 digit PIN: xxxx
- Email: deedelacruz28806@yahoo.com
- I need help with: Forgot password or ID

预先感谢您对我的帮助。

最佳答案

为此,您需要 regex 模块。

x="""CUSTOMER SUPPLIED DATA: 
- topic: Sign in & Password Support
- First Name: Brenda
- Last Name: Delacruz
- Account number: xxxxxxxxx
- U-verse 4-digit PIN: My PIN is
- 4 digit PIN: xxxx
- Email: deedelacruz28806@yahoo.com
- I need help with: Forgot password or ID

*** System::[chat.automatonClientOutcome] Hello! How may I help you today? *** System::[chat.queueWaitDisplayed] We are currently experiencing very high chat volumes which may cause long delays. An agent will be with you as soon as possible.
- topic: Sign in & Password Support
- First Name: Brenda
"""
import regex
print regex.findall(r"CUSTOMER SUPPLIED DATA: \n\K|\G(?!^)(-[^\n]+)\n", x, flags=regex.VERSION1)

输出:['', '- 主题:登录和密码支持', '- 名字:Brenda', '- 姓氏:Delacruz', '- 帐号:xxxxxxxxx', '- U -verse 4 位 PIN 码:我的 PIN 码是'​​, '- 4 位 PIN 码:xxxx', '- 电子邮件:deedelacruz28806@yahoo.com', '- 我需要帮助:忘记密码或 ID']

查看演示。

https://regex101.com/r/naH3C7/2

关于用于模式研究的 Python 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53257610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com