Home > Enterprise >  I want the user to input a list of dictionaries and then will followup and do some searching functio
I want the user to input a list of dictionaries and then will followup and do some searching functio

Time:03-31

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

data to paste = [{'empid': 533952, 'Performance Grade': 'B'}, {'empid': 153989, 'Performance Grade': 'B'}, {'empid': 802554, 'Performance Grade': 'F'}]

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: '))

What i get is: Name type size value performance string 9185 [{'empid': 533952, 'Performance Grade': 'B'}, {'empid':....

What i want is: Name type size value performance list 3 [{'empid': 533952, 'Performance Grade': 'B'}, {'empid':....

CodePudding user response:

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().

  • Related