-1

I'm trying to make a coding system based on Enigma but I'd like to add a twist to it. In order to make to program customizable by the user id like to create "files" wich contains parameters that my prgramm will use to then crypt the message. It includes lists as well as integers. How can I read them?

I have tried using a split method and seperating my lists with "/" but the lists are considered as strings.

Here is an example of the paramaters I would like to assign to, in order, list1, list2, list3, list4, trigger1, trigger2, trigger3 that I tried to seperate with "/":

[6,18,20,12,17,26,19,4,10,22,13,7,14,1,21,9,2,16,3,23,24,8,15,11,25,5]/[1,-5,6,3,-4,2,-4,-4,-3,5,-1,1,-2,-2,-1,3,4,2,5,-4,-4,-4,2,-2,1,5]/[5,-1,-1,-1,-1,1,4,-3,-1,4,1,1,-4,2,-5,4,0,-3,-1,1,-2,0,2,2,-1,-3]/[2,-1,2,0,-3,2,-1,-1,0,2,-1,2,-2,-1,1,4,2,0,-2,-5,2,-1,3,0,-3,-1]/11/5/17

f=open("param.txt")
param=f.read()
list_tbl,param=param.split("/",1)
list_pattern1,param=param.split("/",1)
list_pattern2,param=param.split("/",1)
list_pattern3,param=param.split("/",1)
trigger1,param=param.split("/",1)
trigger2,param=param.split("/",1)
trigger3=param

When I try using the lists, they cannot be used because they are strings.

martineau
  • 99,260
  • 22
  • 139
  • 249
  • 2
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune May 19 '17 at 17:33
  • Possible duplicate of [Convert string representation of list to list in Python](http://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list-in-python) – Tadhg McDonald-Jensen May 19 '17 at 17:55

1 Answers1

1

Have a look at ast.literal_eval which will basically do what you want i.e. make a list out of a string such as this:

from ast import literal_eval
my_string = '[0, 1, 2, 3]'
my_list = literal_eval(my_string)

To create multiple variables you can use a dictionary instead and then just fetch it by a key e.g.:

my_dict = {}
for i in range(10):
    my_dict['list' + str(i)] = <some value>

which is definitely prettier than wasting lines on creating each variable.

Peter Badida
  • 7,432
  • 8
  • 33
  • 74