-1

This is string representation

Data = "[{'Date': '16-Sep-2019', 'Open': 10994.85, 'High': 11052.7, 'Low': 10968.2, 'Close': 11003.5, 'Shares Traded': 434449776, 'Turnover (Rs. Cr)': 15786.17}, {'Date': '17-Sep-2019', 'Open': 11000.1, 'High': 11000.1, 'Low': 10796.5, 'Close': 10817.6, 'Shares Traded': 482013044, 'Turnover (Rs. Cr)': 17721.93}]"

i want it to be list representation

Data = [{'Date': '16-Sep-2019', 'Open': 10994.85, 'High': 11052.7, 'Low': 10968.2, 'Close': 11003.5, 'Shares Traded': 434449776, 'Turnover (Rs. Cr)': 15786.17}, {'Date': '17-Sep-2019', 'Open': 11000.1, 'High': 11000.1, 'Low': 10796.5, 'Close': 10817.6, 'Shares Traded': 482013044, 'Turnover (Rs. Cr)': 17721.93}]

i tried every thing including json.loads(data), Even after json.loads() it shows type as string.Please check in online compiler also. Thankyou

  • 1
    Well eval(string) would technically do it though I'd avoid it for anything but a single use code of my own (or a very trusted source). If that's no good you'd have to know what format of string you were receiving and parse it. – tgrtim Jun 01 '20 at 10:41
  • @tgrtim please help.No its not working with eval. – Pravin Mishra Jun 01 '20 at 10:44
  • You can use the `ast` package. Have a look here https://stackoverflow.com/questions/59607551/how-to-pass-python-arguments-via-start-process-powershell/59607626#59607626 – Amiram Jun 01 '20 at 11:34

2 Answers2

0

You could try this:

L = Data.strip('][').split(', ') 

print(L)
print(type(L))

res = '['+Data.strip('][')+']'

print(res)
print(type(res))
0

Simply use eval function.

data=eval("[{'Date': '16-Sep-2019', 'Open': 10994.85, 'High': 11052.7, 'Low': 10968.2, 'Close': 11003.5, 'Shares Traded': 434449776, 'Turnover (Rs. Cr)': 15786.17}, {'Date': '17-Sep-2019', 'Open': 11000.1, 'High': 11000.1, 'Low': 10796.5, 'Close': 10817.6, 'Shares Traded': 482013044, 'Turnover (Rs. Cr)': 17721.93}]")
print(type(data))
print(data[0])

Output:

<class 'list'>
{'Date': '16-Sep-2019', 'Open': 10994.85, 'High': 11052.7, 'Low': 10968.2, 'Close': 11003.5, 'Shares Traded': 434449776, 'Turnover (Rs. Cr)': 15786.17}