-1

In my application user inputs data which is then used in calculating.

For example, the inputted data is:

[[74, 74], [65, 73], [91, 69]]

And of course, when trying to print(type(data)) I get string type.

I iterate through these arrays so I need it to be list type.

I tried using split/eval, yet no success

#Enter text_to_decipher
  print("Enter your text to decipher or keep the default: ")
  print(text_to_decipher)
  print('\n')

  #start collecting input
  temp_text = ""
  while True:
    temp=input()
    if temp == "":
      break
    else:
      temp_text = temp_text + temp 

  if temp_text != "":
      text_to_decipher = temp_text
  print (text_to_decipher)
  print (type(text_to_decipher))

  text_to_decipher = input()
  print (type(text_to_decipher))

Error that happens when my code tries to iterate through the string and not the list:

Traceback (most recent call last): File "main.py", line 1091, i <module ats1=iter(pair,k[2],iter_function) file "main.py", line 1022, in iter r=M[1] IndexError: string index out of range

Any ideas?

marus
  • 13
  • 3
  • dont lnk your error message to an external site, it's not that long to just cop/paste: `Traceback (most recent call last): File "main.py", line 1091, i – Luuk Oct 13 '19 at 16:05

1 Answers1

0

You can use ast.literal_eval:

import ast

str_data = '[[74, 74], [65, 73], [91, 69]]'
data = ast.literal_eval(str_data)
print(type(data), data)
# <class 'list'> [[74, 74], [65, 73], [91, 69]]
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • Does it change any values when literal_eval is used? Because yes, it converts to a list but I think it changes something in the inside of the list because I am getting ` »»´¿ » ½´° ½¼º ·º§¤ª« ¯­½¥»k»¬½»¬ºª§«´½§¸²¿·` as my decrypted message. Something should be off with int/string values – marus Oct 13 '19 at 16:10
  • Changed a few values and your solution works. Thanks a lot. – marus Oct 13 '19 at 17:11