0

皇冠hg0088代理网址I am using python and have string which is shown below.

str = {"testcase"=>["fail pasword"]}

I want to retrieve the value inside the square brackets. One option is to parse the string and get the data, however here I am looking to read this string and populate the python data structure, so that i can retrieve the value based on the key "testcase".

Wondering what data structure i should use and initialize it with the string value as shown above?

| |
  • 2
    Sorry mate, I’m a bit confused. This isn’t a Python object at all; it resembles a list in a dict, but => isn’t an assignment operator. Where is this coming from. And is the whole statement a string, for which you might use regex to parse ... ? – S3DEV Sep 16 at 22:12
  • ... should this be: str = {"testcase": ["fail pasword"]}? – S3DEV Sep 16 at 22:13
  • If you have a string like str = '{"testcase"=>["fail pasword"]}' then you could do something like str.replace("=>", ":") and then use from AST to parse it as a python dictionary – Dan Sep 16 at 22:13
  • 1
    This look like a ruby hash with a list inside, I would go with Dan comment – geckos Sep 16 at 22:16
  • 1
    @S3DEV literal_eval is perfectly safe, it is not the same as eval, but json.loads would work just as well. – Dan Sep 17 at 0:03

Browse other questions tagged or ask your own question.