I have a line
data = "var1 = {'id': '12345', 'name': 'John White'}"
Is there a way in python to extract var1 as a python variable. More specifically, I am interested in dictionary variables, so that I can get the value of vars: id and name.python
This is the functionality provided by exec
exec
>>> my_scope = {} >>> data = "var1 = {'id': '12345', 'name': 'John White'}" >>> exec(data, my_scope) >>> my_scope['var1'] {'id': '12345', 'name': 'John White'}
You can split the string by =and evaluate the dictionary using the function ast.literal_eval:
=
ast.literal_eval
>>> import ast >>> ast.literal_eval(ata.split('=')[1].strip()) {'id': '12345', 'name': 'John White'}
Source: https://habr.com/ru/post/1626948/More articles:Javascript output - NaN message - javascriptИспользование regex и android для классификации разных полей - javasplitting the contents of a dataframe column into different columns based on values - rWhy, after starting the server socket and using shutdown (), do I have a file in this directory? - cHow to capture server events in knitting - javascriptPhonegap - navigator.app.exitApp () not working - javascript403 при публикации в почтальоне с помощью Yeoman Angular -Fullstack - angularjsWhat does>> = mean in purescript? - purescriptКак извлечь город, страну, широту и долготу из результата google places - google-mapshttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1626953/using-systemjs-to-load-files-for-cordova-app&usg=ALkJrhh3mMUoYvVfjRs8sNPI-s-Rnc_0wQAll Articles