简体   繁体   English

python字符串以更健壮的方式解析数字

[英]python string parse numbers in a more robust way

Got a string after scraping a website.抓取网站后得到一个字符串。

 '<p class="NewsItemContent" style="font-size: 18px;">;As of March 18, 1999, 
6 p.m. Pacific Daylight Time, there are a total of 70;events and 16;planned  
in this area. This total does not include adjacent cities.</p>'

How could I parse out 70, 16. Just want a more robust way.我怎么能解析出 70、16。只是想要一个更健壮的方式。 Wording might change a little bit, but always a total of {};events and {};planned.措辞可能会稍有变化,但始终是 {};events 和 {};planned。 Thanks.谢谢。

Not a very clean solution but here we go:不是一个非常干净的解决方案,但我们开始:

import re

s = ('<p class="NewsItemContent" style="font-size: 18px;">;As of March 18, 1999, '
     '6 p.m. Pacific Daylight Time, there are a total of 70;events and 16;planned  '
     'in this area. This total does not include adjacent cities.</p>')

s = s.split('a total of ')[1]  # split by 'a total of' to get the second part

print(re.findall('\d+', s)[:2])  # finding the first two digits
['70', '16']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM