-3
fn = raw_input("Enter file to open: ")
fileObject = open(fn,'r+')
dictionary = {}
for line in fileObject:
    x = line.split(":")
    a = x[0]
    b = x[1]
    c = len(b)-1
    b = b[0:c]
dictionary[a] = b
print dictionary

After I tested my program, I found out everything were perfect except for 2 tiny issues. Can you please tell me what is wrong?

My text file has the following in it:

username1:password1

username2:password2

username3:password3

username4:password4

username5:password5

(This is an empty line in the actual text file)

First problem: My program reads the file into a dictionary perfectly but it is reading out of order, i tried to print the dictionary after the reading into the dictionary part and below is what I got.

   {'username1': 'password1', 'username3': 'password3', 'username2': 'password2', 'username5': 'password5', 'username4': 'password4'}

Second problem:

for the text file, after password5, I have to hit enter and save the text file to get this:

   {'username1': 'password1', 'username3': 'password3', 'username2': 'password2', 'username5': 'passwor**d5'**, 'username4': 'password4'}

if I don't hit enter at the end of the text file, then it will become this:

    {'username1': 'password1', 'username3': 'password3', 'username2': 'password2', 'username5': 'passwo**rd'**, 'username4': 'password4'}
  • Python dictionaries are not ordered. Read [this](http://stackoverflow.com/a/15479974/1832539) – idjaw Mar 08 '16 at 02:13
  • Use [OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict) instead – mshsayem Mar 08 '16 at 02:13
  • First question is easy: dictionaries can not be sorted. If you want a sorted dictionary, you can use `collections.OrderedDict` – zondo Mar 08 '16 at 02:14
  • BTW, it looks like the indentation of `dictionary[a] = b` is wrong, but I guess that's due to an error in pasting your code here, because otherwise your dictionary would only have the last item from the text file in it. – PM 2Ring Mar 08 '16 at 02:20
  • One question to a question -- anything else is too broad. – Charles Duffy Mar 08 '16 at 02:26
  • Thanks guys, I thought the dictionary in python was ordered, so the problem 1 is solved, but for the second question, if i dont manually press enter at the end of my text file, then the username5:password5 part will be cut off as username5:password. I want to solve this problem. Anyone can help? – Sylvia Wang Mar 08 '16 at 02:37
  • I think i solved the problem, for anyone having same issues, use line= line.strip().split(":") this will do the trick! Thank you all for the help! – Sylvia Wang Mar 08 '16 at 02:42

2 Answers2

0

You are using unordered dictionary, that's why order is not preserved while printing dictionary. Secondly, new line character is needed to iterate correctly using for-in loop.

0

You're mistake is that Dictionaries are not ordered.

I suggest using an OrderedDict instead or using Python lists.

fn = raw_input("Enter file to open: ")
fileObject = open(fn,'r+')
list1 = []

for line in fileObject:
    x = line.split(":")
    list1.append(x)

print list1

Also, what is your intention for the list?