1

How do i convert a string to python dictionary? I get the string in the following format as input from different application.

{ foo1 = [ "User1", "User2" ] bar1 = [ "User3", "User4" ] }
{ foo2 = [ "User5", "User6" ] }
{ foo3 = ["User7", "User8"] bar2 = [ "User9", "User10", "User 11" ] moo = ["User12"] }

I have to convert to the following format in python dictionary.

{ "foo1" : "User1,User2" , "bar1" : "User3,User4" }
{ "foo2" : "User5,User6" }
{ "foo3" : "User7" , "bar2" : "User9,User10,User 11" , "moo" : "User12"}
7
  • 1
    Can you fix the output of that different application so it would use a well-supported text-based serialization format (e.g. JSON, YAML) as it's output instead of it's own, home-cooked one? Commented Sep 25, 2017 at 16:45
  • 2
    Also - the use of comma as a separator doesn't seem consistent between 1st and 3rd example... What's going on there? Commented Sep 25, 2017 at 16:45
  • And bar2 really gets scrunched into one string that's comma separated? Seems odd... Commented Sep 25, 2017 at 16:47
  • ya there are no commas in between keys. Just fixed that. Commented Sep 25, 2017 at 16:47
  • corrected the mistakes Commented Sep 25, 2017 at 16:49

1 Answer 1

1

So you have a third-party application that appears to be supplying you the data in a custom format, and does not support JSON or YAML or anything else conventional.

If that format is indeed completely custom without any serializers/deserializers already available, you have no choice but to write your own parser.

Thus your question becomes “How to easily create a parser for custom formatted data in Python?”

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.