简体   繁体   中英

Python: Finding a line in a file works with windows but not linux

I am attempting to create a piece of code that acts as a dictionary. This is part of a larger project but that seems to be working fine (for now...). I got it working on Windows 10, but once I swapped over to my Ubuntu machine (the end machine where I will run the program), it stopped working with the file I provided. Below is my code for this little bit of fun.

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

The vanillaRequest and clearRequest bits are used because (despite my best efforts) my brain told me to do it similar to other functions I wrote. This was not necessary for this particular function but in others it was essential I preserve the original request.

Giving a bit of probably important information, the dictionary text I am using is the "Webster's Unabridged Dictionary" from the Gutenberg Project. I apologize for not knowing how to upload this file (hopefully you can find it).

The problem seems (to me) to stem from "if (("%s\\n" % word) == line):". I do not know what causes it to work on Windows but not Linux but I can provide a bit of useful information. I created a dummy dictionary with only these lines:

HELL

test definition

HECK

and it worked just as it did on Windows. I'm thinking the problem is with the file but I couldn't find one. Should I be missing any important, mystery solving information, tell me in the comments and I'll add it as soon as I can (if I can)!

If anyone could provide any insight at all it would be well appreciated. Thanks!

PS Yes the code is a bit clunky and a bit of an eyesore. I am not looking to change this as it works perfectly fine as is (besides the whole "Linux doesn't want to play nice with my files" dilemma.

I just test a little, before if (("%s\\n" % word) == line): codes, print word and line variables, word is always uppercase characters,while line is not so. Maybe this is why if statements doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM