Gil*_*ert 2 string python-3.x jupyter
我正在使用 Jupyter 笔记本中的 python 从网络中提取一些数据。我已经提取了数据,解析并创建了数据框。我需要从数据框中的字符串中提取一个数字。我利用这个正则表达式来做到这一点:
for note in df["person_notes"]:
print(re.search(r'\d+', note))
Run Code Online (Sandbox Code Playgroud)
结果如下:
<_sre.SRE_Match object; span=(53, 55), match='89'>
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到比赛号码;在这一行中将是 89。我试图将整行转换为str()和replace(),但并非所有行都具有span=(number, number)相等。先感谢您!
您可以在返回的匹配对象上使用start()和end()方法来获取字符串中的正确位置:
for note in df["person_notes"]:
match = re.search(r'\d+', note)
if match:
print(note[match.start():match.end()])
else:
# no match found ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4100 次 |
| 最近记录: |