gpt4 book ai didi

python - 如何在 excel 或 python 中提取单词周围的文本?

转载 作者:行者123 更新时间:2023-11-28 20:38:16 25 4
gpt4 key购买 nike

我有几千行文字如下:

ksjd 234first special 34-37xy kjsbn
sde 89second special 22-23xh ewio
647red special 55fg dsk
uuire another special 98
another special 107r
green special 55-59 ewk
blue special 31-39jkl

我需要从右边提取“特殊”和数字(或数字范围)之前的一个词。换句话说,我想要:

enter image description here

转换成表格:

enter image description here

最佳答案

一个快速的方法是使用正则表达式:

In [1]: import re

In [2]: text = '''234first special 34-37xy
...: 89second special 22-23xh
...: 647red special 55fg
...: another special 98
...: another special 107r
...: green special 55-59
...: blue special 31-39jkl'''

In [3]: [re.findall('\d*\s*(\S+)\s+(special)\s+(\d+(?:-\d+)?)', line)[0] for line in text.splitlines()]
Out[3]:
[('first', 'special', '34-37'),
('second', 'special', '22-23'),
('red', 'special', '55'),
('another', 'special', '98'),
('another', 'special', '107'),
('green', 'special', '55-59'),
('blue', 'special', '31-39')]

关于python - 如何在 excel 或 python 中提取单词周围的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41417474/

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