-4

I've been working with lists but I've run into an error that I cannot figure out.

The following list works fine:

[{'packetId': 58866707, 'telemetryAt': '2023-05-15T20:22:17', 'telemetryLatitude': 53.907413482666016, 'telemetryLongitude': -122.82716369628906, 'telemetryAltitude': 758,}]

I can, for example, pull a variable with:

telemetry_time = mylist[0]['telemetryAt']

However, I get a base64 message that I have to convert to a string and once I have it, I am having trouble pulling data from the list. I can't seem to convert it to a usable list that I can pull a variable from. Here is the sample string of type string:

{"up":0.088,"h":41.0,"di":0.144}

I tried converting to a string using .split(",") but then I get a weird result of:

['{"up":0.088',"h":41.0',"di":0.144}']

Now - I can clean this up manually but is there a cleaner way to do this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 2
    You likely have a JSON string, so [convert](https://datagy.io/convert-json-to-python-dictionary/) it to a dict using `json.loads(json_data)`. – jarmod May 16 '23 at 19:48
  • Duplicate of [How can I parse (read) and use JSON in Python?](https://stackoverflow.com/questions/7771011/how-can-i-parse-read-and-use-json-in-python) – esqew May 16 '23 at 19:50

2 Answers2

0

My mistake is that I was trying to convert the string to a list. Instead, I needed to convert it to a dictionary.

Originally I had the string of type string: data = {"up":0.088,"h":41.0,"di":0.144} and I tried using:

newdata = list(data.split(",")) which resulted in: print(newdata) outputting ['{"up":0.088,"h":41.0,"di":0.144}'] newdata was of a type list

instead I converted the string data into a dict using:

new_dict = eval(data)

This resulted in:

print(new_dict) {"up":0.088,"h":41.0,"di":0.144} of type 'dict'

This is easy to interrogate and pull variables like:

print(new_dict["di"])

0.144

So - dicts instead of lists in this case!

-1

What are you trying to convert to a string? the variable name? the value assigned to it?

The list you have at the top is a list with a single element, that element being a dictionary that maps variable names to values. So when you do mylist[0]['telemetryAt'], the first set of brackets indexes into the list, and the second performs a dictionary lookup for the key 'telemetryAt', which IS of type "string". As you say, this works.

However, then you refer to the following:

{"up":0.088,"h":41.0,"di":0.144}

as though it were also a string. It's not, however--it's a dictionary with string keys and floating point values--and I don't see how this relates in any way to "mylist". You could certainly perform lookups into this just like with the dict in "mylist", like so:

mydict = {"up":0.088,"h":41.0,"di":0.144}
di_val = mydict["di"] //di_val is now 0.144

There is no need to put it inside a list if that's not already how it is provided to the code you're writing. There also is no reason why you would want to "split" a dictionary on one of its delimiters (which, in any case, are internal details of its printed representation, that you as the USER of this abstraction should not be trying to work with directly)--its key/value pairs must be kept together if it is to function properly. And finally, "split" does NOT make non-strings into strings--in fact it TAKES strings and outputs things that are NOT strings, namely LISTS of strings.

It sounds like you are new to Python and confused about what lists, dicts, and strings are. I recommend you read through the Python tutorials before writing this code--you may fiddle enough to get something that works for the moment for a reason other than why you think it does, and then it mysteriously breaks sometime.

biohacker
  • 142
  • 3
  • I get the error:TypeError Traceback (most recent call last) in () ----> 1 print(text_message["di"]) 2 3 4 TypeError: string indices must be integers – huntthedead May 16 '23 at 21:48
  • The list you are searching is not the same format as the one I am getting from my list. split method. You are using {"up":0.088,"h":41.0,"di":0.144} and I have ['{"up":0.088',"h":41.0',"di":0.144}'] – huntthedead May 17 '23 at 14:22