Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表转换为字符串,同时逐字保存_Python_Regex_Python 2.7 - Fatal编程技术网

Python 将列表转换为字符串,同时逐字保存

Python 将列表转换为字符串,同时逐字保存,python,regex,python-2.7,Python,Regex,Python 2.7,很沮丧地说我在这件事上被难住了。我正在从段落中提取文本: paragraphs = re.findall(r'(<p(.*?)</p>)', html) …我一个字母接一个字母地收到这篇文章,这些单词都被分开了。我是Python新手,这让我很困惑 第一个问题:为什么“段落”不以字符串开头 第二个问题:如何将“段落”转换为字符串,逐字保存,例如: paragraph = ['Two', 'words'] re.findall()返回匹配项列表。您需要re.se

很沮丧地说我在这件事上被难住了。我正在从段落中提取文本:

    paragraphs = re.findall(r'(<p(.*?)</p>)', html)
…我一个字母接一个字母地收到这篇文章,这些单词都被分开了。我是Python新手,这让我很困惑

第一个问题:为什么“段落”不以字符串开头

第二个问题:如何将“段落”转换为字符串,逐字保存,例如:

    paragraph = ['Two', 'words']
re.findall()
返回匹配项列表。您需要
re.search()

不过,更好的选择是使用HTML解析器,如:

>>来自bs4导入组
>>> 
>>>数据=“这里有一些文本

” >>>soup=BeautifulSoup(数据,“html.parser”) >>>soup.p.get_text().split() [u'some',u'text',u'here']
re.findall()
返回匹配项列表。您需要
re.search()

不过,更好的选择是使用HTML解析器,如:

>>来自bs4导入组
>>> 
>>>数据=“这里有一些文本

” >>>soup=BeautifulSoup(数据,“html.parser”) >>>soup.p.get_text().split() [u'some',u'text',u'here']
    paragraphs = str(paragraphs)
    paragraph = ['Two', 'words']
>>> from bs4 import BeautifulSoup
>>> 
>>> data = '<p>some text here</p>'
>>> soup = BeautifulSoup(data, "html.parser")
>>> soup.p.get_text().split()
[u'some', u'text', u'here']