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']"} 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, andNone.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 .
s.replace('https://','') it’s better s[len('https://'):] to remove the prefix string . - jfs{"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'] \' 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' Source: https://ru.stackoverflow.com/questions/582152/
All Articles
словарь['https']. Maybe specify what you want? - andreymal