By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,872 Members | 679 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,872 IT Pros & Developers. It's quick & easy.

change a text file to a dictionary

P: 1
I have a text file, which contains a list of element and their weight, and i need to split the list up and sort it by the weight of the elements. So i need
to change the weight to a float.

The text file:
Ac 227.0
Ag 107.868
Al 26.98154
Am 241.1
Ar 39.948
As 74.9216
...

So far i have done this:
Expand|Select|Wrap|Line Numbers
  1. # ----The data structure----
  2. # The input file
  3. text_file=open("avikt.txt","r")  
  4. Lines = text_file.readlines()    
  5. text_file.close()
  6. # The Lines get broken into columns and in 1 list "Element"
  7. Element = map(string.split,Lines) # now Element is a nested list ...
  8.                                               # [['Ac','227.0'],['Ag','107.868'],...
  9.  
  10. # The problem now is to change the atom weight into a float
  11. # to sort it.
  12.  
  13. # I make the Element list into a dictionary
  14. # where the Atom is the key and the atom weight
  15. # is the value
  16. d  = dict(Element)
  17.  
  18. # now i have to sort the dictionary by the value, or the atom weight
  19. items = [(v,k) for k, v in d.items()]
  20. items.sort()
  21. items = [(k,v) for v, k in items]
Can anybody help me??
May 2 '06 #1
Share this Question
Share on Google+
5 Replies

bartonc
Expert 5K+
P: 6,596
Hey, guys...

This is an old post, but has been viewed over 500 times.
That's over 500 people looking for an answer, and if they had found one, may have joined the forum. I'll dig up old posts from time to time just for this reason.

I have a text file, which contains a list of element and their weight, and i need to split the list up and sort it by the weight of the elements. So i need
to change the weight to a float.

The text file:
Ac 227.0
Ag 107.868
Al 26.98154
Am 241.1
Ar 39.948
As 74.9216
...

So far i have done this:
Expand|Select|Wrap|Line Numbers
  1. # ----The data structure----
  2. # The input file
  3. text_file=open("avikt.txt","r")  
  4. Lines = text_file.readlines()    
  5. text_file.close()
  6. # The Lines get broken into columns and in 1 list "Element"
  7. Element = map(string.split, Lines) # now Element is a nested list ...
  8.                                               # [['Ac','227.0'],['Ag','107.868'],...
  9.  
  10. # The problem now is to change the atom weight into a float
  11. # to sort it.
  12.  
  13. # I make the Element list into a dictionary
  14. # where the Atom is the key and the atom weight
  15. # is the value
  16. d  = dict(Element)
  17.  
  18. # now i have to sort the dictionary by the value, or the atom weight
  19. items = [(v,k) for k, v in d.items()]
  20. items.sort()
  21. items = [(k,v) for v, k in items]
Can anybody help me??
Sorting a dictionary doesn't work (they are not ordered lists). The use of map() is quite good though.
Apr 7 '07 #2

Expert 100+
P: 511
one example, although the format of the float values may not be what OP wants
Expand|Select|Wrap|Line Numbers
  1. import operator
  2. data = open("file").readlines()
  3. data = [i.split() for i in data]
  4. data = [ [k[0],float(k[1]) ] for k in data]
  5. data = sorted(data, key=operator.itemgetter(1)) #sort by 2nd element
  6. data = [ [j[0],"%.5f" % j[1]]  for j in data]
  7. print data
  8.  
output
Expand|Select|Wrap|Line Numbers
  1. [['Al', '26.98154'], ['Ar', '39.94800'], ['As', '74.92160'], ['Ag', '107.86800'], ['Ac', '227.00000'], ['Am', '241.10000']]
  2.  
Apr 7 '07 #3

bartonc
Expert 5K+
P: 6,596
one example, although the format of the float values may not be what OP wants
Expand|Select|Wrap|Line Numbers
  1. import operator
  2. data = open("file").readlines()
  3. data = [i.split() for i in data]
  4. data = [ [k[0],float(k[1]) ] for k in data]
  5. data = sorted(data, key=operator.itemgetter(1)) #sort by 2nd element
  6. data = [ [j[0],"%.5f" % j[1]]  for j in data]
  7. print data
  8.  
output
Expand|Select|Wrap|Line Numbers
  1. [['Al', '26.98154'], ['Ar', '39.94800'], ['As', '74.92160'], ['Ag', '107.86800'], ['Ac', '227.00000'], ['Am', '241.10000']]
  2.  
You are THE MAN, GD. It makes me wish that I weren't so busy creating GUI apps. I'd love to know the Global Module Index the way that you do (I've never even looked at the operator module before).

Thanks!
Apr 7 '07 #4

Expert 100+
P: 511
hi, if you are interested, here's the documentedlink that i got it from.
Apr 7 '07 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Nice solution ghostdog74. Like Barton, I am not very familar with the operator module other than to access new division (operator.truediv()). For the exercise, here's another approach:
Expand|Select|Wrap|Line Numbers
  1. data = [item.split() for item in open('file_name').readlines()]
  2. dataDict = dict(zip([float(i[1]) for i in data], [j[0] for j in data]))
  3. print dataDict
  4.  
  5. outList = [[dataDict[key], '%01.5f' % key] for key in sorted(dataDict.keys())]
  6. print outList
  7.  
  8. '''
  9. >>> {39.948: 'Ar', 227.0: 'Ac', 241.09999999999999: 'Am', 26.981539999999999: 'Al', 74.921599999999998: 'As', 107.86799999999999: 'Ag'}
  10. [['Al', '26.98154'], ['Ar', '39.94800'], ['As', '74.92160'], ['Ag', '107.86800'], ['Ac', '227.00000'], ['Am', '241.10000']]
  11. >>> 
  12. '''
I looked up operator.itemgetter(item):
'Creates a callable object, f, where a call to f(obj) returns obj[item].'
Apr 7 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.