[Tutor] Building dictionary from large txt file

avi.e.gross at gmail.com avi.e.gross at gmail.com
Tue Jul 26 20:47:06 EDT 2022


There is so much work to be done based on what you show and questions to
answer.

The short answer is you made ONE dictionary and overwrote it. You want an
empty dictionary that you keep inserting this dictionary you made into.

You need to recognize when a section of lines is complete. When you see a
blank line now, you PASS. 

Your goal seems to be to read in a multi-line entry perhaps between dividers
like this"--------" so your readlines may not be doing what you want as each
lines has a single item and some may have none.

Whatever you read in seems to be in content. Your code wrapped funny on MY
screen so I did not see this line:

isotope_data={'Z':0,'A':0,'m':0}#start to create subdictionary for each case
of atoms with its unique keys and values 
for line in content:
    data=line.strip().split()

OK, without comment, the rest of your code seems to suggest there is perhaps
a blank line between a series of lines containing info.

It is of some concern that two entries now start with Atomic but you only
look for one.

And are you aware that the split leaves these as single entities:
1.00782503223(9), 0.999885(70), [1.00784,1.00811]

Those are all TEXT and you seem to want to remove things in parentheses from
your output. Do you really want to generally store text or various forms of
numbers?

You have lots of work to make the details work such as by concatenating what
in your code was NOT READ containing an "H" with the isotopic number of "1"
into H1. 

It is best to consider reading in a small sample of ONE and making the code
work to extract what is needed and combine it into the form you want and
make a single entry. Then add a loop. If your data is guaranteed to always
have the same N lines, other methods may work as well or better. 

-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
bobx ander
Sent: Tuesday, July 26, 2022 4:58 PM
To: tutor at python.org
Subject: [Tutor] Building dictionary from large txt file

Hi all,
I'm trying to build a dictionary from a rather large file of following
format after it has being read into a list(excerpt from start of list below)
--------

Atomic Number = 1
    Atomic Symbol = H
    Mass Number = 1
    Relative Atomic Mass = 1.00782503223(9)
    Isotopic Composition = 0.999885(70)
    Standard Atomic Weight = [1.00784,1.00811]
    Notes = m
--------

My goal is to extract the content into a dictionary that displays each
unique triplet as indicated below
{'H1': {'Z': 1,'A': 1,'m': 1.00782503223},
              'D2': {'Z': 1,'A': 2,'m': 2.01410177812}
               ...} etc
My code that I have attempted is as follows:

filename='ex.txt'

afile=open(filename,'r') #opens the file
content=afile.readlines()
afile.close()
isotope_data={'Z':0,'A':0,'m':0}#start to create subdictionary for each case
of atoms with its unique keys and values for line in content:
    data=line.strip().split()

    if len(data)<1:
        pass
    elif data[0]=="Atomic" and data[1]=="Number":
        atomic_number=data[3]


     elif data[0]=="Mass" and data[1]=="Number":
        mass_number=data[3]



    elif data[0]=="Relative" and data[1]=="Atomic" and data[2]=="Mass":
        relative_atomic_mass=data[4]


isotope_data['Z']=atomic_number
isotope_data['A']=mass_number
isotope_data['A']=relative_atomic_mass
isotope_data

the output from the programme is only

{'Z': '118', 'A': '295', 'm': '295.21624(69#)'}

I seem to be owerwriting each dictionary and ends up with the above
result.Somehow i think I have to put the assigment of the key,value pairs
elsewhere.

I have tried directly below the elif statements also,but that did not work.

Any hints or ideas

Regards

Bob
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list