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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/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_File Io_Special Characters - Fatal编程技术网

Python中字符串匹配的问题

Python中字符串匹配的问题,python,string,file-io,special-characters,Python,String,File Io,Special Characters,我试图从文件中读取并匹配某个字符串组合。PFB我的计划: def negative_verbs_features(filename): # Open and read the file content file = open (filename, "r") text = file.read() file.close() # Create a list of negative verbs from the MPQA lexicon file_ne

我试图从文件中读取并匹配某个字符串组合。PFB我的计划:

def negative_verbs_features(filename):

    # Open and read the file content
    file = open (filename, "r")
    text = file.read()
    file.close()

    # Create a list of negative verbs from the MPQA lexicon
    file_negative_mpqa = open("../data/PolarLexicons/negative_mpqa.txt", "r")
    negative_verbs = []
    for line in file_negative_mpqa:
        #print line,
        pos, word = line.split(",")
        #print line.split(",")      
        if pos == "verb":
            negative_verbs.append(word)
    return negative_verbs

if __name__ == "__main__":
    print negative_verbs_features("../data/test.txt")
文件negative_mpqa.txt由单词、词性标记对组成,标记对之间用逗号(,)分隔。以下是该文件的一个片段:

abandoned,adj
abandonment,noun
abandon,verb
abasement,anypos
abase,verb
abash,verb
abate,verb
abdicate,verb
aberration,adj
aberration,noun
我想创建一个文件中所有单词的列表,其中动词作为词性。但是,当我运行程序时,返回的列表(否定动词)总是空的。if循环没有执行。我试着通过取消对打印行的注释来打印word、pos对。split(“,”)PFB是输出的一个片段

['wrongful', 'adj\r\n']
['wrongly', 'anypos\r\n']
['wrought', 'adj\r\n']
['wrought', 'noun\r\n']
['yawn', 'noun\r\n']
['yawn', 'verb\r\n']
['yelp', 'verb\r\n']
['zealot', 'noun\r\n']
['zealous', 'adj\r\n']
['zealously', 'anypos\r\n']
我知道我的文件可能有一些特殊字符,比如换行符和每行末尾的返回提要。我只想忽略它们,建立我的列表。请告诉我如何进行


PS:我是Python新手。

替换行
pos,word=line.split(“,”)
by

word, pos = line.rstrip().split(",")
rstrip()删除字符串右侧的白色字符(空格、新行、回车…)。请注意,lstrip()甚至strip()也存在。您还切换了word和pos


在将word变量附加到列表中时,也可以对其使用rstrip()。

将行
pos,word=line.split(“,”)替换为

word, pos = line.rstrip().split(",")
rstrip()删除字符串右侧的白色字符(空格、新行、回车…)。请注意,lstrip()甚至strip()也存在。您还切换了word和pos


当您将word变量附加到列表中时,您也可以在word变量上使用rstrip()。

您说该文件有如下行:
放弃,adj
,因此这些是
word,pos
对。但是你写了
pos,word=line.split(“,”)
,这意味着
pos=='废弃的'
word=='adj'
。。。我想现在很清楚为什么列表会是空的:-)

你说文件中有这样的行:
放弃,adj
所以这些是
单词,pos
对。但是你写了
pos,word=line.split(“,”)
,这意味着
pos=='废弃的'
word=='adj'
。。。我想现在很清楚为什么这个列表会是空的:-)文森特·萨瓦德,谢谢你的回复。我照你说的做了,但还是不走运!打印行.split(“,”)的输出仍然是['drinked',adj\r\n']['drinked',noon\r\n']['yawn',noon\r\n']['yawn',verb\r\n']['yelp',verb\r\n']['zealot',noon\r\n']['zealous',adj\r\n']['zeally',anypos r\r\n']我读得太快了,我的坏了。在代码中,变量pos包含单词(即:yawn)和单词(即类型)(即:动词)。你得换一下。我编辑。请注意,如果不想看到这些内容,仍然必须使用strip()\r\n!文森特/托尼,谢谢。仅使用THC4k和rstrip()所起的作用。Vincent Savard,感谢您的回复。我照你说的做了,但还是不走运!打印行.split(“,”)的输出仍然是['drinked',adj\r\n']['drinked',noon\r\n']['yawn',noon\r\n']['yawn',verb\r\n']['yelp',verb\r\n']['zealot',noon\r\n']['zealous',adj\r\n']['zeally',anypos r\r\n']我读得太快了,我的坏了。在代码中,变量pos包含单词(即:yawn)和单词(即类型)(即:动词)。你得换一下。我编辑。请注意,如果不想看到这些内容,仍然必须使用strip()\r\n!文森特/托尼,谢谢。仅使用THC4k和rstrip()所起的作用。