There is a dictionary in this format. Tell me how to get out of it all the elements?

{'https': "https://['123.240.106.33:8998', '200.54.180.226:80', '186.103.201.74:8080']"} 
  • Here is one key string and one string value that can be pulled out via the словарь['https'] . Maybe specify what you want? - andreymal
  • @andreymal 123.240.106.33:8998 ',' 200.54.180.226:80 ',' 186.103.201.74:8080 I want to pull out exactly these elements - mastermind1337

3 answers 3

You can use ast.literal_eval () :

 In [140]: d = {'https': "https://['123.240.106.33:8998', '200.54.180.226:80', '186.103.201.74:8080']"} In [141]: import ast In [142]: ast.literal_eval(d['https'].replace('https://','')) Out[142]: ['123.240.106.33:8998', '200.54.180.226:80', '186.103.201.74:8080'] 

or prescription @jfs :

 In [305]: ast.literal_eval(d['https'][len('https://'):]) Out[305]: ['123.240.106.33:8998', '200.54.180.226:80', '186.103.201.74:8080'] 

From the documentation:

ast.literal_eval (node_or_string) Unicode or Latin-1 encoded string containing a Python literal or container display. The following Python literal structures: strings , numbers , tuples , lists , dicts , booleans , and None .

It is used as a guideline. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

ast.literal_eval(node_or_string) is a function to safely evaluate (evaluate) a string, i.e. Python string will be "parsed" and executed. Only works for strings containing the following structures: strings , numbers , tuples , lists , dicts , booleans , and None .

  • one
    1- It is worth mentioning briefly in Russian what ast.literal_eval () does. 2- instead of s.replace('https://','') it’s better s[len('https://'):] to remove the prefix string . - jfs
  • @jfs, thanks! Completed the answer ... - MaxU
  • The meaning of literal_eval is to recognize constants (Python literal) in a string, how they are written in the code, that is: {"abc", 123, (),[],{'a': True}, None} . But literal_eval will not understand exactly the same object, if it is not in the form of constants specified, for example: set([]) . In general, it is better to use more portable formats such as json instead of literal_eval - jfs
 # при условии что в качестве разделителя в list не " или ''' кавычки dt = {'https': "https://['123.240.106.33:8998', '200.54.180.226:80', '186.103.201.74:8080']"} res = dt['https'].split("'")[1::2] # или res = dt['https'][10:-2].split("', '") >>> ['123.240.106.33:8998', '200.54.180.226:80', '186.103.201.74:8080'] 
  • elegantly turned out! - MaxU
  • @MaxU is the first \' in the line to silently break the code. Errors must be loud so that data does not lose. - jfs
 d = {'https': "https://['123.240.106.33:8998', '200.54.180.226:80', '186.103.201.74:8080']"} for ip in eval(d['https'].split('https://')[1]): print(ip) >>> '123.240.106.33:8998' >>> '200.54.180.226:80' >>> '186.103.201.74:8080' 
  • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky
  • one
    Does it happen if the script code gets into that list? eval can do it - gil9red