在Python中使用Split()从字符串中提取所有引号

时间:2018-11-06 23:43:09

标签: python split

所以我想做的是使用python来解析有关所有引号的文章。我用漂亮的汤从网站上提取了html,现在我正尝试使用split打印引号中的所有内容。

例如,来自:

I like quotes but especially "have problems"

have problems

2 个答案:

答案 0 :(得分:2)

re.findall(r'"([^"]*)"',s),演示:

>>> import re
>>> s='I like quotes but especially "have problems"'
>>> re.findall(r'"([^"]*)"',s)
['have problems']
>>> 

regex是您的好朋友,

  

https://docs.python.org/3/howto/regex.html

     
    

https://docs.python.org/3/library/re.html?highlight=findall#re.findall

  

答案 1 :(得分:1)

您可以切片str.split返回的列表:

s = 'I like quotes but especially "have problems" and "need more quotes"'
s.split('"')[1::2]

这将返回:

['have problems', 'need more quotes']
相关问题