Python Forum
Populating a list with Dictionaries....
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populating a list with Dictionaries....
#1
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?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks for your input.
I tried it but it made no change.
Reply
#4
It's working for me. Did you replace it in both places (lines 13 and 38)?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Yes I did.
Then I added more prints to figure out what was happening - And the it worked...

Thanks for your help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help comparing totals from list of dictionaries AnOddGirl 1 1,560 Mar-18-2020, 01:17 AM
Last Post: AnOddGirl
  dictionaries and list as values Pippi 6 3,485 Apr-13-2019, 09:05 AM
Last Post: perfringo
  Newbie to Python - Problem in accessing Dictionaries and List sambill 1 3,064 Aug-17-2017, 07:38 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020