[Tutor] Fwd: arrangement of datafile

Keith Winston keithwins at gmail.com
Mon Jan 6 04:04:29 CET 2014


Amrita, on a closer read of your very first post I (think I) see you
already successfully read your data into a series of dicts (mylist in your
example), so if you still want the output you posted in the first post,
then you can do some version of the loops that I described. That said, I'm
sure Stephen is right about the csv module having helpful tools for parsing
that original file, if you're not past that point. Anyway, if you had all
your mylist in a list of lists (NOT a dict, as you have them) then getting
the output you wanted seems easy. HOWEVER: I don't think you want to strip
off the first two numbers, since I think the first one is critical to your
final listing, no? I might be beating a tired horse here, but you could get
your desired output from such a list of lists like this (I modified the
names, and added back in that first number to the output)

for i in mylist_list:
    for atom in mylist_list[i][2]:
        print(mylist_list[i][0], atom, " = ",
mylist_list[i][2][atom])      # output something like "2 C = 178.255"

This is pretty ugly, but you get the idea. As I mentioned before, putting
it all into a class would allow you to do this more robustly, but I'll
probably lead you astray if I expand on that.

Keith
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140105/143b8d0b/attachment.html>


More information about the Tutor mailing list