我有一份 Excel 表格中的客户服务调用日志。以下是我的数据格式
So# Comments
1 sjhsh QUOTE 234566
1 sdsds customer call QUote 239876 Call back
2 adsdfh unknown call from customer QUOTE 189067 sdkjsd woieweio
3 QUOTE 657894 customer called for service
我正在从 Excel 中读取此数据,需要获取每行中文本“QUOTE”后面的 6 位数字,然后将提取的数字添加为新列
1.The rows might have multiple "QUOTE" mentions 2.The rows might not have "QUOTE"at all
有人可以帮我使用 python 进行子字符串搜索吗
import pandas as pd
import re
file=pd.read_excel("C:/Users/rkatta/Desktop/Book1.xlsx")
file.set_index('Index', inplace=True, drop=True)
comments=file['InternalComments']
quotenum=[]
keyword= 'QUOTE'
for i in comments:
try:
befor_keyowrd, keyword, after_keyword = comments[i].partition(keyword)
num=after_keyword[:6]
quotenum.append(num)
except AttributeError:
befor_keyowrd, keyword, after_keyword =''
quotenum.append(after_keyword)
最佳答案
(?i)(?<=QUOTE )\d+
将捕获您正在寻找的数字。
(?i)
表示模式的其余部分不区分大小写,因此它将匹配“QUote”以及该单词的任何变体。
(?<=QUOTE )
表示数字前面将带有引号和空格
\d+
是你的电话号码
关于python - 在数据帧列中查找字符串 "Quote"之后的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51159898/