1

I'm new to Python, so any help would be appreciated.

I wrote the following code in a class on a PC, and it worked without any problems.

word = raw_input('Word: ')
f = open('dictionary.txt','r')
for line in f:
    line = line.strip()
    #print(line)
    if sorted(line) == sorted(word):
        print(line)
f.close()

When I put the same thing onto a Mac, the lines still have a "\" at the end (i.e. "apple\"), and so aren't matching what my input was. Is there something I'm missing?

Julien
  • 13,986
  • 5
  • 29
  • 53
  • 2
    Could you give an example please? – Julien Aug 04 '16 at 00:24
  • 1
    try with `line.strip('\n')` – levi Aug 04 '16 at 00:27
  • The dictionary is a .txt file with words listed one per line. Running the code, it will prompt "Word: " and store the input (I tested with "apple" in various forms containing all the letters but not necessarily in the proper order. After that, the code doesn't print anything. When I took off the # from the print(line) line, it would print all of the words in the .txt file, then not print anything else. All the words from the .txt file it printed were printed with the "\" at the end, i.e. "apple\", "orange\", etc. I tried adding '\n' to to the line.strip(), but that still didn't do it. –  Aug 04 '16 at 01:53
  • Try adding `print(repr(line))` before you strip to see what characters are actually read from the file. It should show `'apple\r'` or `'orange\r'`. – Arcege Aug 04 '16 at 02:10
  • So, I have no clue how it worked, but I redid the dictionary.txt document using Textwrangler, saved to the same folder, and it works now. Maybe I mixed something up when I created that file. Thanks, I do appreciate the ideas! –  Aug 04 '16 at 02:20
  • @andrew TextWrangler likely changed the line ending characters in your data file. – Feneric Aug 04 '16 at 03:14

0 Answers0