我抓了几篇文章并将它们收集在一个文本文件中。从这些文章中我想提取所有用大写字母写的单词:
这是收集的其中一篇文章的样本:
"|[<p>Advertisement , By Milan Schreur and Alissa J. Rubin OCT. 5, 2016
, BRUSSELS — A man wounded two police officers with a knife in Brussels around noon on Wednesday in what the authorities called “a potential terrorist attack.” , ....]"
我要提取的是所有那些在大写锁定中输入的单词,在这种情况下,单词&#34; BRUSSELS&#34;,但在文件中还有更多。所以我想提取完全由大写字母组成的所有单词。
我试过这样的事情:
import re
text_open = open("Training_News_6.csv")
text_read = text_open.read()
articles = text_read.split("<p>")
pattern = re.findall("\s[A-Z]{4,}\s",'\n'.join(articles))
print(pattern)
答案 0 :(得分:0)
articles
之后, text_read.split
是一个列表。 re.findall
期待一个字符串。