We have upgraded the community system as part of the upgrade a password reset is required for all users before login in.

[Solved] Python - Dictionary?

  • I am stuck - supposedly this line (given from ubus -S call gps info written to a file) is in the correct format to be considered a "dictionary object" by python. Is there something else I should be using?


    However, when I try and read out the data with the following script:
    #!/usr/bin/env python

    gpsfile=open('/root/gpsinfo.txt', 'r')


    print "Age: "+age
    print "Latitude: "+lat
    print "Latitude: "+lon
    print "Longitude: "+elv
    print "Course: "+cou
    print "Speed: "+spd


    I just get the following error:
    Traceback (most recent call last):
    File "test.py", line 10, in <module>
    TypeError: string indices must be integers, not str

  • gpsfile=open('/root/gpsinfo.txt', 'r')

    read() method returns string here, as you are simply reading a file.

    If this is the actual data from the file, with double quotes around key names and values:


    Then you can use json to load it up. This should work:

    #!/usr/bin/env python
    import json
    from pprint import pprint
    with file('/root/gpsinfo.txt', 'r') as gpsfile:
        gpsinfo = json.load(gpsfile)
    age = gpsinfo['age']
    lat = gpsinfo['latitude']
    lon = gpsinfo['longitude']
    elv = gpsinfo['elevation']
    cou = gpsinfo['course']
    spd = gpsinfo['speed']
    pprint(("Age: " + age,
            "Latitude: " + lat,
            "Latitude: " + lon,
            "Longitude: " + elv,
            "Course: " + cou,
            "Speed: " + spd))

  • Thank you, thank you, thank you! I have been spinning my wheels for hours now. That works, and I have incorporated it into my actual script.

    May even throw it up as a project!

  • @Brad-Buskey Your welcome. šŸ™‚ And share your projects!

    P.S. fixed a typo in my code sample.

  • This post is deleted!

Looks like your connection to Community was lost, please wait while we try to reconnect.