gpt4 book ai didi

python - 从 python 中的字符串中提取特定模式

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:44 24 4
gpt4 key购买 nike

我的 Dataframe 列中有以下数据(包含大约 100 行)。

需要从每行的 DF 中提取 CK 字符串 (CK-36799-1523333)。

  • 注意:receipt_id不固定。Ck数据可能包含在一些不同的地方变量。

数据:

{"currency":"US","Cost":129,"receipt_id":"CK-36799-1523333","af_customer_user_id":"33738413"}

{"currency":"INR","Cost":429,"receipt_id":"CK-33711-15293046","af_customer_user_id":"33738414"}

{"currency":"US","Cost":229,"receipt_id":"CK-36798-1523333","af_customer_user_id":"33738423"}

{"currency":"INR","Cost":829,"receipt_id":"CK-33716-152930456","af_customer_user_id":"33738214"}

{"currency":"INR","Cost":829,"order_id":"CK-33716-152930456","af_customer_user_id":"33738214"}

{"currency":"INR","Cost":829,"suborder_id":"CK-33716-152930456","af_customer_user_id":"33738214"}

结果

CK-36799-1523333
CK-33711-15293046
CK-36798-1523333
CK-33716-152930456

我尝试了 str.find('CK-') 函数,但没有得到预期结果。需要建议

最佳答案

尝试使用正则表达式

import re

...
for line in data:
res = re.findall(r"CK\-[0-9]+\-[0-9]+", line)
if len(res) != 0:
print(res[0])

关于python - 从 python 中的字符串中提取特定模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55022587/

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