Populating a list with Dictionaries.... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Populating a list with Dictionaries.... (/thread-13013.html) |
Populating a list with Dictionaries.... - SteenRudberg - Sep-23-2018 HI I have a .adi file that looks like this: <PROGRAMID:15>WWFFlog by 5p0o <PROGRAMVERSION:4>5.04 <EOH> ***************** START OF NEW CALL: 5P0O <OPERATOR:4>5P0O <STATION_CALLSIGN:9>OZ4WWFF/P <CALL:5>ON4CB <QSO_DATE:8>20170322 <TIME_ON:4>1508 <RST_SENT:2>59 <RST_RCVD:2>52 <BAND:3>80M <MODE:3>SSB <MY_SIG:9>OZFF-0175 <EOR> <OPERATOR:4>5P0O <STATION_CALLSIGN:9>OZ4WWFF/P <CALL:5>OZ1QZ <QSO_DATE:8>20170322 <TIME_ON:4>1509 <RST_SENT:2>59 <RST_RCVD:2>41 <BAND:3>80M <MODE:3>SSB <MY_SIG:9>OZFF-0175 <EOR> <OPERATOR:4>5P0O <STATION_CALLSIGN:9>OZ4WWFF/P <CALL:5>LX1CC <QSO_DATE:8>20170322 <TIME_ON:4>1512 <RST_SENT:2>59 <RST_RCVD:2>57 <BAND:3>80M <MODE:3>SSB <MY_SIG:9>OZFF-0175 <EOR> <OPERATOR:4>5P0O <STATION_CALLSIGN:9>OZ4WWFF/P <CALL:6>SP8LEP <QSO_DATE:8>20170322 <TIME_ON:4>1513 <RST_SENT:2>52 <RST_RCVD:2>55 <BAND:3>80M <MODE:3>SSB <MY_SIG:9>OZFF-0175 <EOR> I want to read and store with in my program. My code look like this: loggen = [] qsotom = {} x = 0 # garbage def key_ok(nøgle): if nøgle == 'OPERATOR' or nøgle == 'STATION_CALLSIGN' or nøgle == 'CALL' or nøgle == 'QSO_DATE' or nøgle == 'TIME_ON' or nøgle == 'RST_SENT' or nøgle == 'RST_RCVD' or nøgle == 'BAND' or nøgle == 'MODE' or nøgle == 'MY_SIG': return True else: return False def read_adif(filename): logg = [] qso = qsotom index = 0 with open(filename) as input_file: for line in input_file: try: if line.index('OPERATOR') > -1: qso_data = True except: qso_data = False if qso_data: #qso = qsotom arr = line.split('<') for txt in arr: txt2 = txt.split('>') txt3 = txt.split(':') try: print('t2={},t3={}'.format(txt2[1], txt3[0])) except: x = 0 try: if key_ok(txt3[0]) and txt3[0] != 'EOR>': qso[txt3[0]] = txt2[1].strip() elif txt3[0].strip() == 'EOR>': logg.append(qso) qso = qsotom index = index + 1 except: x = 0 # End of with return logg loggen = read_adif('20170322_16_05_28_OZ4WWFF-P_OZFF-0175.ADI') i = 0 while i < længde: print(loggen[i]) print('\n') i += 1 I have added some print.... to see that data is ok during running. When I do the print on "loggen[i]" the 3 first entries are the same as the last appended. What am I doing wrong? RE: Populating a list with Dictionaries.... - ichabod801 - Sep-24-2018 When you do qso = qsotom , qso basically becomes a pointer to the same dictionary qsotom is pointing at. It doesn't become a new dictionary. You need to make a new dictionary each time. Try using qso = {} instead.
RE: Populating a list with Dictionaries.... - SteenRudberg - Sep-24-2018 Thanks for your input. I tried it but it made no change. RE: Populating a list with Dictionaries.... - ichabod801 - Sep-24-2018 It's working for me. Did you replace it in both places (lines 13 and 38)? RE: Populating a list with Dictionaries.... - SteenRudberg - Sep-26-2018 Yes I did. Then I added more prints to figure out what was happening - And the it worked... Thanks for your help |