Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Resource Cleanup - Fatal编程技术网

Python中的文本清理

Python中的文本清理,python,string,resource-cleanup,Python,String,Resource Cleanup,我是Python新手,无法找到删除无用文本的方法。主要目的是保留我想要的单词,并删除所有其他单词。在这个阶段,我可以检查我的in_data并找到我想要的单词。如果句子。查找(wordToCheck)为正,则保留它。INU数据中的是每行一句,但当前输出是每行一个字。我想要的是保留格式,找到每行中的单词并删除其余的 import Orange import orange word = ['roaming','overseas','samsung'] out_data = [] for i in

我是Python新手,无法找到删除无用文本的方法。主要目的是保留我想要的单词,并删除所有其他单词。在这个阶段,我可以检查我的in_data并找到我想要的单词。如果句子。查找(wordToCheck)为正,则保留它。INU数据中的是每行一句,但当前输出是每行一个字。我想要的是保留格式,找到每行中的单词并删除其余的

import Orange
import orange

word = ['roaming','overseas','samsung']
out_data = []

for i in range(len(in_data)):
    for j in range(len(word)):
        sentence = str(in_data[i][0])
        wordToCheck = word[j]
        if(sentence.find(wordToCheck) >= 0):
            print wordToCheck
输出

roaming
overseas
roaming
overseas
roaming
overseas
samsung
samsung
数据中的类似于句子

contacted vodafone about going overseas and asked about roaming charges. The customer support officer says there isn't a charge but while checking my usage overseas.
我希望看到输出是这样的

overseas roaming overseas

您可以做得更简单,如下所示:

for w in in_data.split():
    if w in word:
        print w
在这里,我们首先将
in_data
按空格分割,返回一个单词列表。然后,我们循环遍历in数据中的每个单词,并检查该单词是否与您要查找的单词中的一个相等。如果有,我们就打印出来

而且,为了更快地查找,请将
单词
-列表改为一个集合。快得多

此外,如果要处理标点和符号,需要使用正则表达式或检查字符串中的所有字符是否都是字母。因此,要获得所需的输出:

import string
in_words = ('roaming','overseas','samsung')
out_words = []

for w in in_data.split():
    w = "".join([c for c in w if c in string.letters])
    if w in in_words:
        out_words.append(w)
" ".join(out_words)

您可以为此使用正则表达式:

>>> import re
>>> word = ['roaming','overseas','samsung']
>>> s =  "Contacted vodafone about going overseas and asked about roaming charges. The customer support officer says there isn't a charge but while checking my usage overseas."
>>> pattern = r'|'.join(map(re.escape, word))
>>> re.findall(pattern, s)
['overseas', 'roaming', 'overseas']
>>> ' '.join(_)
'overseas roaming overseas'
非正则表达式的方法是使用
str.join
str.strip
以及生成器表达式。需要使用strip()调用来去除标点符号,如
'、
、'

>>> from string import punctuation
>>> ' '.join(y for y in (x.strip(punctuation) for x in s.split()) if y in word)
'overseas roaming overseas'
这里有一个更简单的方法:

>>> import re
>>> i
"Contacted vodafone about going overseas and asked about roaming charges. The customer support officer says there isn't a charge but while checking my usage overseas."
>>> words
['roaming', 'overseas', 'samsung']
>>> [w for w in re.findall(r"[\w']+", i) if w in words]
['overseas', 'roaming', 'overseas']

使用split的答案会出现在标点符号上。你需要用一个正则表达式来分解单词

import re

in_data = "contacted vodafone about going overseas and asked about roaming charges. The customer support officer says there isn't a charge but while checking my usage overseas."

word = ['roaming','overseas','samsung']
out_data = []

word_re = re.compile(r'[^\w\']+')
for check_word in word_re.split(in_data):
  if check_word in word:
    print check_word

in_数据是这样的一句话:“联系沃达丰关于出国事宜,询问漫游费用。客户支持人员说没有费用,但在检查我在海外的使用情况时。”我期望的输出是“海外漫游海外”这是什么。。什么是你的数据?请分享你的数据。我的数据正好相反:)我的意思是,你可以用正则表达式。但这并不完全是“最佳实践”或pythonic,特别是对于这样一个简单的问题。然而,我承认,我并没有把它看作是字符串中的双关语和符号。在这种情况下,这并不太愚蠢:-)谢谢你的帮助,我对此有了一些想法。这不会提供所需的输出。
In_words
应该是一个更快查找的集合,即使用
In_words=set(['roaming'、'overseas'、'samsung'])
,iirc元组查找比列表稍慢。我使用橙色的python脚本,在我尝试这个之后,它给出了AttributeError:'Orange.data.Table'没有属性'split'>>>@user3705931:我不知道什么是
Orange
。但是数据中的
是字符串吗?如果
.split()
不起作用,您需要告诉我们有关数据中
的更多信息。@BurhanKhalid:是吗?