数字的正则表达式findall

2024-05-18 23:42:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图提取嵌入文本中的6位数字。数字总是以0开头,长度总是6位数,在第4位数后用句点隔开,如下所示:

0 0133.02[text] in location [texttext](text) numbers 1 0121.08[text] in location [texttext](text) numbers ...

我运行以下内容:

import re filtered = re.findall("0\d\d\d[.]\d\d", str(df['col']))

共有478行要解析,每行都包含所述数字。但是,filtered结果只输出60,即使我更改了regex格式。有趣的是,filtered似乎主要由478行的第一行和最后几行的数字组成,但不是中间的数字?在

编辑: 我提取了有效行和不工作行,发现有效的行是前30行和后30行(0-29448-477)。在

以下是不工作的行的示例(446447): 446 0005.00 [CT] in Vancouver [CMA] (B.C.) 44160 447 0170.05 [CT] in Vancouver [CMA] (B.C.) 44006

还有一个行的示例(448449): 448 0050.04 [CT] in Vancouver [CMA] (B.C.) 43995 449 0067.01 [CT] in Vancouver [CMA] (B.C.) 43989


Tags: textin文本re示例数字locationfiltered
1条回答
网友
1楼 · 发布于 2024-05-18 23:42:18

这些东西可以帮助你解决这个问题。我会删除这个,因为它不是一个答案。在

import re
import pandas as pd

data = dict(col=['texttexttext 0036.01 texttext','texttexttext 0006.21 texttext'])
df = pd.DataFrame(data)

re.findall("0\d{3}\.\d{2}", str(df['col'])) #Simplified your regex

有效地创造:

^{pr2}$

试试这个怎么样:

re.findall("0\d{3}\.\d{2}",' '.join(df['col'].tolist()))

如果中间行不起作用,提取一个样本,并与我们分享:

print('\n'.join(df['col'][200:220].tolist()))

相关问题 更多 >

    热门问题