从匹配regex numpy fromregex的行加载数字数据

2024-09-29 23:26:19 发布

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

我正在尝试从具有以下格式的文本文件加载浮点数:

# random line of text
# random line of text
# keywordtomatch wordtext, wordtext, wordtext with spaces, 751791.000000, text with alphanumeric characters
# keywordtomatch wordtext, wordtext, wordtext with spaces, 751791.000000, text with alphanumeric characters
# random line of text
# random line of text

到目前为止,我无法让以下/类似的正则表达式适用于我:

^{pr2}$

从只包含keywordtomatch的行中读取浮点数。试图调整我的regex在regex101上没有成功。在

这看起来很简单,但对于像我这样的非裸体经验丰富的人来说不行:),那么一条经验丰富的python可能可以帮助我使用一行代码?在

请假定关键字ToMatch未出现在文件的其他位置。在


Tags: oftext格式withlinerandomspaces文本文件
1条回答
网友
1楼 · 发布于 2024-09-29 23:26:19

从小处开始:

In [58]: txt = "# keywordtomatch wordtext, wordtext, wordtext with spaces, 751791.000000, text with alphanumeric characters"
In [59]: re.match('# keywordtomatch (\w+)', txt[3])
In [60]: re.match('# keywordtomatch (\w+)', txt)
Out[60]: <_sre.SRE_Match object; span=(0, 25), match='# keywordtomatch wordtext'>
In [64]: _.groups()
Out[64]: ('wordtext',)

让我们简化文本:

^{pr2}$

现在将txt行复制到一个文件中

In [80]: dt = np.dtype('U10,U10,int,U10')
In [81]: np.fromregex('stack42659805.txt', pat, dtype=dt)
Out[81]: 
array([('word', 'another', 123, 'word')], 
      dtype=[('f0', '<U10'), ('f1', '<U10'), ('f2', '<i4'), ('f3', '<U10')])

它适用于多行匹配的行,跳过不匹配的行

所以剩下的问题就是找出一个正确的模式。在

概括一下:

In [89]: re.match('start (\w+), ([\w ]+), ([\d\.]+), (\w+)', 'start one, two words, 3.4, four')
Out[89]: <_sre.SRE_Match object; span=(0, 31), match='start one, two words, 3.4, four'>
In [90]: _.groups()
Out[90]: ('one', 'two words', '3.4', 'four')

In [91]: pat = 'start (\w+), ([\w ]+), ([\d\.]+), (\w+)'
In [95]: dt = np.dtype('U10,U20,float,U10')
In [96]: np.fromregex('stack42659805.txt', pat, dtype=dt)
Out[96]: 
array([('word', 'another',  123.  , 'word'),
       ('word', 'another word',  123.  , 'word'),
       ('word', 'another and more',  123.43, 'word')], 
      dtype=[('f0', '<U10'), ('f1', '<U20'), ('f2', '<f8'), ('f3', '<U10')])

相关问题 更多 >

    热门问题