0

I have a script running for network devices configurations. there are different username and password combinations to login, so I have file of username and password, reading into dictionary.

I put the most used username and password combination in 1. line and I am expecting script to try this line first but it won't work. python seems to create dictionary like selecting lines randomly. and that costs me extra time to connect devices because sometimes it takes up to 4 tries to find correct username while the correct one is first line :)

What I mean:

file

username1:password1
username2:password2
username3:password3

What I expect to see: {username1:password1, username2:password2, username3:password3}

What happens: {username3:password3, username1:password1, username2:password2}

The part creating dictionary

with open('userandpass.txt', 'r') as f:
        for line in f:
                user, pwd = line.strip().split(':')
                credentials[user] = pwd

Thank you guys! Used ordered dictionary, sorry for posting duplicate question.

  • take a look at: [Why is the order in dictionaries and sets arbitrary?](http://stackoverflow.com/questions/15479928/why-is-the-order-in-dictionaries-and-sets-arbitrary) – Moinuddin Quadri Nov 19 '16 at 19:38
  • Dictionaries are unordered. Either use `sorted(...)`, use `OrderedDict`, or wait until Python 3.6. – L3viathan Nov 19 '16 at 19:38

0 Answers0