读取文件导致的错误

时间:2016-03-02 20:27:30

标签: python regex file-io

我正面临一个相当难以捉摸的错误,这似乎是由于从文件中读取而引起的。 我简化了我的程序来演示这个问题:

认为这个程序运行良好:

import re

sourceString="Yesterday I had a pizza for lunch it was tasty\n";
sourceString+="today I am thinking about grabbing a burger and tomorrow it\n"; 
sourceString+="will probably be some fish if I am lucky\n\n\n";
sourceString+="see you later!"

jj=["pizza","steak","fish"]

for keyword in jj:
    regexPattern= keyword+".*";
    patternObject=re.compile(regexPattern,re.MULTILINE);
    match=patternObject.search(sourceString);
    if match:
        print("Match found for "+keyword)
        print(match.group()+"\n")
    else:
        print("warning: no match found for :"+ keyword+"\n")

我正在使用一个非常简单的正则表达式模式,但我从我的数组jj获得正则表达式的要点

脚本按预期工作(匹配模式包含"披萨""鱼"但不匹配"牛排")

现在在我的实际程序中,我试图从文件中读取这些关键字(我不想在源代码中进行硬编码)

到目前为止,我有这个:

import re

sourceString="Yesterday I had a pizza for lunch it was tasty\n";
sourceString+="today I am thinking about grabbing a burger and tomorrow it\n"; 
sourceString+="will probably be some fish if I am lucky\n\n\n";
sourceString+="see you later!"

with open('keyWords.txt','r') as f: 
    for keyword in f:
        regexPattern= keyword+".*";
        patternObject=re.compile(regexPattern,re.MULTILINE);
        match=patternObject.search(sourceString);
        if match:
            print("Match found for "+keyword)
            print(match.group())
        else:
            print("warning: no match found for :"+ keyword)

其中keyWords.txt将包含以下内容:

pizza
steak
fish

但是这会破坏代码,因为只有文件中的LAST关键字才能成功匹配(如果存在匹配)。

是什么给出了?

1 个答案:

答案 0 :(得分:3)

with open('keyWords.txt','r') as f: 
    for keyword in f:
        regexPattern = keyword.strip() + ".*";

使用strip()删除keyword中的所有newline个字符。如果您确定无法获得任何领先的空白,rstrip()就足够了。