'I want the user to input a list of dictionaries and then will followup and do some searching functions, however userinput becomes a string
I want the user to input something like (refer below to data to paste) and assign it to the value performance directly as a list of dictionaries and this will be assigned to a variable called performance.
I do not want to make the user individually add item by item and update it into a list, they should just paste the data below
i want to use pure python or some light library like sys (built in only allowed), please help
What I have written below gives me an error when trying to extract values etc. since it is automatically stored as a string:
performance = (input('Enter your database as a list of dictionaries: '))
Solution 1:[1]
Use the function eval(), which parsed and evaluated the string provided in python syntax:
inp = input('paste :')
data = eval(inp)
print(repr(data))
p.s. Beware of code injection while using eval().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ytung-dev |