0

I've followed the first answer on this thread:

Python - file to dictionary?

And whenever I try and run the script, Python just closes. everything, even my other scripts I'm not working on.

Here's what I've got written, its virtually the same:

    d = {}
with open("C:\Users\Owatch\Documents\Python\FunStuff\nsed.txt") as f:
    for line in f:
        (key, val) = line.split()
        d[int(key)] = val

print(d)

The only thing I changed was the file location as that's what I understood I was to include to fix the error about not finding the file

Elaboration:

Here is the code I am supposed to use:

d = {}
with open("file.txt") as f:
    for line in f:
       (key, val) = line.split()
       d[int(key)] = val

Here is what I did, adding a file path in place of file.txt, and having it so that it would or should print the dictionary d as soon as it's done.

d = {}
with open("C:\Users\Owatch\Documents\Python\Unisung Net Send\nsed.txt") as f:
    for line in f:
        (key, val) = line.split()
        d[int(key)] = val

print(d)

The problem is I cannot even run this, as Python just crashes, I am running version: 3.1

Community
  • 1
  • 1
Micrified
  • 3,338
  • 4
  • 33
  • 59
  • can you please elaborate. Looking at your code, it seems that you have an indentation error. – GodMan Feb 17 '13 at 10:48
  • 4
    Are you running this on Windows? If so, by 'just crashing' you mean the command window closes again? – Martijn Pieters Feb 17 '13 at 10:54
  • See [How to stop command prompt from closing in python?](http://stackoverflow.com/questions/14142509/14142564#14142564) if that is the case. – Martijn Pieters Feb 17 '13 at 10:56
  • And last but not least, does your input have one space per line, or more? – Martijn Pieters Feb 17 '13 at 10:57
  • Just a wold guess. The full file path contains \n. in the file name: nsed.txt. Try using os.path.join – GodMan Feb 17 '13 at 10:57
  • What I mean is that when I try and run it in IDLE, it simply crashes. I am not using the command prompt. My text file looks like this: https://docs.google.com/file/d/0BwYE5uc9NjE-cEo2VzIzb0IzdmM/edit?usp=sharing And I am not sure what you mean about the file path GodMan (I am running this on Windows 7) – Micrified Feb 17 '13 at 11:02
  • You should probably make that file public. Try using http://gist.github.com/ – Will Feb 17 '13 at 11:06
  • @GodMan Could you explain what you mean with the file path? – Micrified Feb 17 '13 at 11:15
  • I think the answer below by 'Martijn Pieters' explains that – GodMan Feb 17 '13 at 11:16
  • 1
    When you say 'crash' in that case you most probably mean there is a traceback. Next time, please include that traceback in your questions. – Martijn Pieters Feb 17 '13 at 11:34

3 Answers3

1

Change

open("C:\Users\Owatch\Documents\Python\FunStuff\nsed.txt")

to

open(r"C:\Users\Owatch\Documents\Python\FunStuff\nsed.txt")

Otherwise "\nsed" will be taken as a newline plus "sed".

Update:

From your input file, problem is:

d[int(key)] = val

Because your first column are letters, not integers. Change it to:

d[key] = val

Or: (if you prefer number keys)

d[ord(key) - ord('a')] = val
Hui Zheng
  • 10,084
  • 2
  • 35
  • 40
  • I got this error: Traceback (most recent call last): File "C:\Users\Owatch\Documents\Python\FunStuff\File Import", line 5, in d[int(key)] = val ValueError: invalid literal for int() with base 10: 'a' – Micrified Feb 17 '13 at 11:12
  • Fixed. Thank you, if you could help me a little more and explain exactly what is going on please? I hate it when I don't understand what is being undertaken. IF you don't want to, its all cool. I am probably asking a little much. Thanks for solving the problem – Micrified Feb 17 '13 at 11:19
  • No problem. `int(x)` converts a string or number `x` to an integer. Since your first column are all letters, it fails to do so. If you don't care the numbers, just remove `int`. If you prefer number keys, `ord` will give you integer ordinal of a letter, and `ord(key) - ord('a')` will give you the index starting from letter 'a'. – Hui Zheng Feb 17 '13 at 11:24
0

Use a r'' raw string literal to prevent Python from interpreting \n as a newline:

with open(r"C:\Users\Owatch\Documents\Python\Unisung Net Send\nsed.txt") as f:

or use double backslashes:

with open("C:\\Users\\Owatch\\Documents\\Python\\Unisung Net Send\\nsed.txt") as f:

or forward slashes instead:

with open(r"C:/Users/Owatch/Documents/Python/Unisung Net Send/nsed.txt") as f:

All three versions are valid on Windows.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Change d[int(key)] to d[ord(key)]

GodMan
  • 2,561
  • 2
  • 24
  • 40
  • What is the difference from using d[ord(key)] from using d[key] = val – Micrified Feb 17 '13 at 11:23
  • And what if I want my second column to be numbers, and not numbers in ' ' ? – Micrified Feb 17 '13 at 11:25
  • ord('a') gives you 97. This 97 will be considered by the hashing function inside python's dictionary d, so it can act as a key – GodMan Feb 17 '13 at 11:26
  • d[key] directly takes python's help to provide 'a' to the hashing function, so that 'a' acts as the key. So, while referring to the value given to the ley 'a' in this case, you will use d['a']. In the other case, you will use d[ord('a')] or d[97] – GodMan Feb 17 '13 at 11:27
  • 1
    Referring to the answer by 'Hui ZHeng', d[ord(key) - ord('a')] = val means that d[0] is called for 'a', d[1] for 'b' and similarly d[25] for 'z' – GodMan Feb 17 '13 at 11:30
  • @Owatch: The second column in your file is a float number in string form. So, as an answer to your question regarding second column as numbers, you might want to use float(val) instead of just val. Alos, it would only matter when you are calculating something with them, but, for the dictionary d, it doesnt matter – GodMan Feb 17 '13 at 11:33