user2624229
user2624229

Reputation: 43

extract a dictionary key value from a string

I am currently in the process of using python to transmit a python dictionary from one raspberry pi to another over a 433Mhz link, using virtual wire (vw.py) to send data.

The issue with vw.py is that data being sent is in string format.

I am successfully receiving the data on PI_no2, and now I am trying to reformat the data so it can be placed back in a dictionary.

I have created a small snippet to test with, and created a temporary string in the same format it is received as from vw.py

So far I have successfully split the string at the colon, and I am now trying to get rid of the double quotes, without much success.

my_status = {}
#temp is in the format the data is recieved
temp = "'mycode':['1','2','firstname','Lastname']"

key,value = temp.split(':')
print key
print value

key = key.replace("'",'')
value = value.replace("'",'')

my_status.update({key:value})

print my_status   

Gives the result

'mycode'
['1','2','firstname','Lastname']
{'mycode': '[1,2,firstname,Lastname]'}

I require the value to be in the format

['1','2','firstname','Lastname']

but the strip gets rid of all the single speech marks.

Upvotes: 3

Views: 3380

Answers (3)

en_Knight
en_Knight

Reputation: 5381

Alternatively to cricket_007's suggestion of using a syntax tree parser - you're format is very similar to the standard yaml format. This is a pretty lightweight and intutive framework so I'll suggest it

a = "'mycode':['1','2','firstname','Lastname']"
print yaml.load(a.replace(":",": "))
# prints the dictionary {'mycode': ['1', '2', 'firstname', 'Lastname']}

The only thing that's different between your format and yaml is the colon needs a space

It also will distinguish between primitive data types for you, if that's important. Drop the quotes around 1 and 2 and it determines that they're numerical.

Tadhg McDonald-Jensen suggested pickling in the comments. This will allow you to store more complicated objects, though you may lose the human-readable format you've been experimenting with

Upvotes: 0

sulimmesh
sulimmesh

Reputation: 763

This shouldn't be hard to solve. What you need to do is strip away the [ ] in your list string, then split by ,. Once you've done this, iterate over the elements are add them to a list. Your code should look like this:

string = "[1,2,firstname,lastname]"
string = string.strip("[")
string = string.strip("]")
values = string.split(",")
final_list = []
for val in values:
    final_list.append(val)
print final_list

This will return:

> ['1','2','firstname','lastname']

Then take this list and insert it into your dictionary:

 d = {}
 d['mycode'] = final_list

The advantage of this method is that you can handle each value independently. If you need to convert 1 and 2 to int then you'll be able to do that while leaving the other two as str.

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191743

You can use ast.literal_eval

import ast
temp = "'mycode':['1','2','firstname','Lastname']"
key,value = map(ast.literal_eval, temp.split(':'))
status = {key: value}

Will output

{'mycode': ['1', '2', 'firstname', 'Lastname']}

Upvotes: 2

Related Questions