I am using a txt file to create a dictionary. However the dictionary is not sorted according to the txt file no matter the sequence of values in the txt file. Is there a way to sort the file or to sort the dictionary according alphabetically ascending (A to Z) or in the order it is in the file? Thanks!
Here is the file:
And here is what I get, no matter the order of items in the file:
{'Weslaco': 'WE', 'San Augustine': 'ST', 'Temple': 'TE', 'San Angelo': 'SG'}
Code:
file = open('D:\\ArcGISDATA\\SARS\\OfficeDomain_list.txt', 'r')
foresterdomaintxt = {}
for line in file.readlines():
x = line.split(",")
a = x[0]
b = x[1]
bstrip =x[1].strip('\n')
foresterdomaintxt[a]=bstrip
print foresterdomaintxt
Code:
Temple,TE
San Angelo,SG
San Augustine,ST
Weslaco,WE
{'Weslaco': 'WE', 'San Augustine': 'ST', 'Temple': 'TE', 'San Angelo': 'SG'}