【问题标题】:Python: Finding a line in a file works with windows but not linuxPython:在文件中查找一行适用于 windows 但不适用于 linux
【发布时间】:2016-07-04 15:57:21
【问题描述】:

我正在尝试创建一段充当字典的代码。这是一个更大项目的一部分,但似乎工作正常(现在......)。我让它在 Windows 10 上运行,但是一旦我切换到我的 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 位,是因为(尽管我尽了最大努力)我的大脑告诉我这样做类似于我编写的其他函数。对于这个特定的功能,这不是必需的,但在其他功能中,我必须保留原始请求。

提供一些可能很重要的信息,我使用的字典文本是来自 Gutenberg Project 的“Webster's Unabridged Dictionary”。我很抱歉不知道如何上传这个文件(希望你能找到它)。

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

HELL

test definition

HECK

它就像在 Windows 上一样工作。我认为问题出在文件上,但我找不到。如果我遗漏了任何重要的解谜信息,请在 cmets 中告诉我,我会尽快添加(如果可以的话)!

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

附:是的,代码有点笨拙,有点碍眼。我不打算改变它,因为它可以正常工作(除了整个“Linux 不想和我的文件一起玩”的困境。

【问题讨论】:

  • 如果没有输入样本,我发现很难遵循您的逻辑。作为一个快速的猜测,也许你的问题与空白有关。对于您怀疑的行,也许您应该尝试: if word.strip() == line.strip(): 这与检查大致相同,只是它不关心空格,包括开头或结尾处的换行符和回车符词或行。您最好在字符串上使用默认的 split(),而不是像在函数顶部附近那样在单个空间上进行拆分。但为了确定任何事情,我认为您需要提供输入样本。
  • @AndrewAllaire ,快速编辑代码,到目前为止它可以工作。我会更多地测试它,但我认为你解决了它!你会在这背后有什么解释吗?我很好奇为什么会发生这种情况(我有点难过修复如此简单而且我没有想到它)。非常感谢!
  • file.readline "从文件中读取一整行。在字符串中保留一个尾随换行符(但当文件以不完整的行结尾时可能不存在)。"
  • file.open "在 Windows 上,模式后附加 'b' 以二进制模式打开文件,因此也有 'rb'、'wb' 和 'r+b' 等模式。 Windows 上的 Python 对文本文件和二进制文件进行了区分;在读取或写入数据时,文本文件中的行尾字符会自动轻微改变。"
  • 我不知道是不是你用的版本,但是this version\r\n 行尾。 Python 2 使用 C FILE 流,Windows CRT 实现 [t]ext 和 [b]inary 模式,文本模式是默认模式。文本模式将行尾转换为\n。 Linux等POSIX系统一般只以二进制方式打开文件。但是在 Python 2 中,如果您使用 dictionary = open("dictionary.txt", "rU") 打开文件,您可以获得 [U] 通用换行模式。

标签: python linux windows python-2.7 dictionary


【解决方案1】:

我只是测试了一下,在if (("%s\n" % word) == line):codes之前,打印wordline变量,word总是大写,而line不是。也许这就是为什么if 语句不起作用的原因。

【讨论】:

  • 字典文件中定义前的单词总是大写。该行不是因为某些行是纯粹的定义。该语句所做的是遍历并找到“word”变量的完全匹配。这是定义的开始。
猜你喜欢
  • 1970-01-01
  • 2011-03-09
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 2015-06-22
相关资源
最近更新 更多