I have a list as a string:
"['USA', 'Canada', 'Mexico', 'Brazil']"
I want to get a list from a string, for example:
['USA', 'Canada', 'Mexico', 'Brazil']
How can i do this? Itertools or a list function did not give me the correct result. Thanks
you can evaluate the row in the list using eval:
eval
eval('["USA", "Canada", "Mexico", "Brazil"]')
Doing this in a python shell gives a list:
>>> eval('["USA", "Canada", "Mexico", "Brazil"]') ['USA', 'Canada', 'Mexico', 'Brazil'] >>>
You can use literal_evalfrom the module ast, as in this example:
literal_eval
ast
from ast import literal_eval as le data = le('["USA", "Canada", "Mexico", "Brazil"]') print(data)
Conclusion:
Also, do not use eval(), use literal_eval(). See more details.
eval()
literal_eval()
Source: https://habr.com/ru/post/1682289/More articles:transforming a peaceful time sequence in time - phpOpenGL 4.5 - mock buffer shader storage layout - c ++How to use paginator from angular material? - angularПакет для прокрутки всех файлов с предопределенным списком или набором расширений и игнорирование всех других типов файлов - windowsWhy is my smallIcon for notifications always grayed out? - androidDifferent random numbers at the same time - randomCount sequences of numbers - rThreshold in Android using opencv - androidOpenCV4Android - blank image after using MorphologyEX - androidAdding in Java 8 using BinaryOperator - javaAll Articles