我试着从一个包含一个字符的单词的数据框中打印出句子,不管它是句子的开头、中间还是结尾,我试着的代码是
lookfor = '[' + re.escape("A-Za-z") + ']'
tdata = pd.read_csv(fileinput, nrows=0).columns[0]
skip = int(tdata.count(' ') == 0)
tdata = pd.read_csv(fileinput, names=['sentences'], skiprows=skip)
filtered = tdata[tdata.sentences.str.contains(lookfor, regex=True, na=False)]
print(filtered)
#a sample set
-----------------------------
#hi, how are; you z
#im w good thanks
#How am I
#good, what about you
#my name is alex
#K hello, alex how are you !
#it is a car
#great news
#thanks!
-----------------------------
expected output
-----------------------------
#hi, how are; you z
#im w good thanks
#How am I
#K hello, alex how are you !
#it is a car
-----------------------------
即使我在lookfor数组中写下了所有的字母,它也不起作用。它将打印包含这些字母的任何句子。不是当它们单独出现时。有什么想法吗
将^{} 与一个具有单词边界的单词一起使用,并按^{} 过滤:
编辑:对于排除
A
和I
,您可以在比较之前使用replace
:或:
尝试:
产出:
相关问题 更多 >
编程相关推荐