OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Parsing string to dictionary

  • Thread starter Thread starter AmericanJael
  • Start date Start date
A

AmericanJael

Guest
I'm working on a communications project with a radio that transmits a formatted string message, similar to:

Code:
message_string = 'Transmission\n variables \n  0.01 First variable\n  0.02 Second variable\n  0.03 Third variable \n More variables\n  0.03 Next variable\n  0.04 Another variable'

When printed, this looks like

Code:
print(message_string)
Transmission
 variables
  0.01 First variable
  0.02 Second variable
  0.03 Third variable
 More variables
  0.03 Next variable
  0.04 Another variable

This looks nice to humans, but is tricky for the computer - especially since I am trying to convert this to a python dictionary. In my actual system there are quite a few of these variables, and the code needs to systematically process all of them into a dictionary.

I think it might include something like

Code:
message_string = message_string.replace('\n','{')

but deciding which direction of brackets to use in different cases, and where to put the colons for the dictionary, is confusing me. I want an output similar to

Code:
message_dict = {
    'variables': {
       'First variable': 0.01,
       'Second variable': 0.02,
        'Third variable': 0.03},
    'More variables': {
       'Next variable': 0.03,
       'Another variable': 0.04,
    } 
}

where an error would not be thrown if one of the variables was missing from the transmission (since that sometimes happens).How do I convert this string into a dictionary?
<p>I'm working on a communications project with a radio that transmits a formatted string message, similar to:</p>
<pre><code>message_string = 'Transmission\n variables \n 0.01 First variable\n 0.02 Second variable\n 0.03 Third variable \n More variables\n 0.03 Next variable\n 0.04 Another variable'
</code></pre>
<p>When printed, this looks like</p>
<pre><code>print(message_string)
Transmission
variables
0.01 First variable
0.02 Second variable
0.03 Third variable
More variables
0.03 Next variable
0.04 Another variable
</code></pre>
<p>This looks nice to humans, but is tricky for the computer - especially since I am trying to convert this to a python dictionary. In my actual system there are quite a few of these variables, and the code needs to systematically process all of them into a dictionary.</p>
<p>I think it might include something like</p>
<pre><code>message_string = message_string.replace('\n','{')
</code></pre>
<p>but deciding which direction of brackets to use in different cases, and where to put the colons for the dictionary, is confusing me. I want an output similar to</p>
<pre><code>message_dict = {
'variables': {
'First variable': 0.01,
'Second variable': 0.02,
'Third variable': 0.03},
'More variables': {
'Next variable': 0.03,
'Another variable': 0.04,
}
}
</code></pre>
<p>where an error would not be thrown if one of the variables was missing from the transmission (since that sometimes happens).How do I convert this string into a dictionary?</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top