Python:在文件中查找行适用于windows,但不适用于linux

Python:在文件中查找行适用于windows,但不适用于linux,python,linux,windows,python-2.7,dictionary,Python,Linux,Windows,Python 2.7,Dictionary,我正在尝试创建一段充当字典的代码。这是一个更大项目的一部分,但似乎工作良好(目前…)。我让它在Windows10上运行,但一旦我切换到我的Ubuntu机器(我将运行该程序的终端机器),它就停止使用我提供的文件。下面是我为这一点点乐趣编写的代码 def commandDefineWord(request): # NOT WORKING vanillaRequest = request.split(' ') clearRequest = (request.lower(

我正在尝试创建一段充当字典的代码。这是一个更大项目的一部分,但似乎工作良好(目前…)。我让它在Windows10上运行,但一旦我切换到我的Ubuntu机器(我将运行该程序的终端机器),它就停止使用我提供的文件。下面是我为这一点点乐趣编写的代码

def commandDefineWord(request):         # NOT WORKING
    vanillaRequest = request.split(' ')
    clearRequest = (request.lower()).split(' ');

    if ((clearRequest[0] == "what") and (clearRequest[1] == "is") and (clearRequest[2] == "the")
        and (clearRequest[3] == "definition") and (clearRequest[4] == "for")):
        del clearRequest[0];
        del vanillaRequest[0];
        del clearRequest[0];
        del vanillaRequest[0];
        del clearRequest[0];
        del vanillaRequest[0];
        del clearRequest[0];
        del vanillaRequest[0];      # Lots of extra word screening
        del clearRequest[0];
        del vanillaRequest[0];
    if (clearRequest[0] == "define"):
        del clearRequest[0];
        del vanillaRequest[0];
    if ((clearRequest[0] == "the") and (clearRequest[1] == "word")):
        del clearRequest[0];
        del vanillaRequest[0];
        del clearRequest[0];
        del vanillaRequest[0];

    word = (vanillaRequest[0]).upper(); # define the word as a variable
    word = word.replace("?", "");       # remove ?'s
    dictionary = open("dictionary.txt", "r");   # Open dictionary file
    searchLines = dictionary.readlines();   # create a readable copy
    dictionary.close(); # Close file for safety

    found = False       # set as precaution to not finding word
    for i, line in enumerate(searchLines):  # enumerating all the lines while making them individual
        if (("%s\n" % word) == line):   # where is the bloody word?
            found = True;   # word is found
            print searchLines[i];   # print result
            i+=1;   # go to next line of definition
            while not searchLines[i].isupper(): # check to see if defintion end is reached
                searchLines[i] = searchLines[i].replace("\n", "");  # take away new lines
                print "\t %s" % searchLines[i]; # prints defintion
                i += 1;
    if found == False:
        print "Sorry, I could not find a definition for %s." % word
之所以使用vanillaRequest和clearRequest位,是因为(尽管我尽了最大努力)我的大脑告诉我要做的事情与我编写的其他函数类似。对于这个特定的功能,这不是必需的,但在其他功能中,我必须保留原始请求

我使用的词典文本是古腾堡项目的“韦伯斯特未删节词典”,提供了一些可能很重要的信息。我很抱歉不知道如何上传这个文件(希望你能找到它)

(在我看来)这个问题似乎源于“如果((%s\n”%word)=行):”。我不知道是什么原因导致它在Windows上工作,但不是Linux,但我可以提供一些有用的信息。我创建了一个只有以下几行的虚拟字典:

HELL

test definition

HECK
它的工作原理和在Windows上一样。我想问题出在文件上,但我找不到。如果我遗漏了任何重要的、解开谜团的信息,请在评论中告诉我,我会尽快添加(如果可以的话)

如果有人能提供任何见解,我们将不胜感激。谢谢


是的,代码有点笨重,有点碍眼。我不想改变这一点,因为它工作得非常好(除了整个“Linux不想很好地处理我的文件”的困境。

if((%s\n”%word)==line)之前,我只做了一点测试:
code,print
word
line
变量,word总是大写字符,而line却不是。这也许就是为什么
如果
语句不起作用的原因。

我发现没有输入样本就很难理解您的逻辑。快速猜测,可能您的问题与空格有关。对于这行,您是可疑的关于也许您应该尝试:if word.strip()==line.strip():这是大致相同的检查,只是它不关心空格,包括换行符和单词或行开头或结尾的回车符。您可能最好使用默认的拆分()在字符串上,而不是像在函数顶部那样在单个空格上拆分。但为了确保一切,我认为您需要提供一个输入示例。@AndrewAllaire,代码的快速编辑,到目前为止,它是有效的。我将进一步测试它,但我认为您解决了它!您是否有任何解释?我是curious解释了为什么会发生这种情况(我有点难过,因为修复太简单了,我没有想到)。非常感谢!“从文件中读取一整行。字符串中保留了一个尾随的换行符(但当文件以不完整的行结尾时,可能不存在尾随的换行符)。”“在Windows上,附加在模式后的‘b’以二进制模式打开文件,因此也有‘rb’、‘wb’和‘r+b’等模式。Windows上的Python对文本文件和二进制文件进行了区分;在读取或写入数据时,文本文件中的行尾字符会自动轻微更改。“我不知道它是否是您使用的版本,但有
\r\n
行结尾。Python 2使用C
文件
流,Windows CRT实现[t]ext和[b]inary模式,默认为text模式。text模式将行尾转换为
\n
。POSIX系统(如Linux)通常仅以二进制模式打开文件。但在Python 2中,如果使用
dictionary=open(“dictionary.txt”,“rU”)打开文件,则可以获得[U]通用换行符模式
。单词在字典文件中的定义之前总是大写。这一行不是因为某些行纯粹是定义。语句所做的是遍历并找到“word”变量的精确匹配项。这是定义的开始。