hello
hello

Reputation: 137

python: dictionary and if statements

I have a file to read and a dictionary. If the file says:

sort-by username

I want the dictionary to be

d = {'sort-by': 'username'}

if the file says :

sort-by name

then I want the dictionary to be

d = {'sort-by': 'name'}

right now, I have:

if 'user' in line:
    d['sort-by'] = 'username'
else:
    d['sort-by'] = 'name'

however, even though the file says sort-by username, I keep getting

d = {'sort-by': 'name'}

why is this?

Upvotes: 0

Views: 43

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113978

print dict(map(str.split,open("some_file.txt")))

assuming your file actually looks like your example

if you have any control it may be more appropriate to store your data as json

Upvotes: 3

Related Questions