0

with python 2.7 and configparser module is it possible to get data in list format?

i.e

config = ConfigParser() 
config.read( "BACK.ini")                 # read my input file
dizP=dict(config.items("sezione"))       # assign data to dictionary

dizP = {                                                                 
       "lista1": lista, 
       "lista2": lista1
        ...
       }

problem is data in lista1 and lista2 are seen as string so if I do:

print lista1[1:10]

this return the first ten char int the list, not the comma separated values

How can I get the list format for the read data from config parser?

user2239318
  • 2,578
  • 7
  • 28
  • 50
  • 2
    If the elements in the value are comma separated you can just do: `dizP = {k:val.split(',') for k,val in config.items("sezione")}`. – Bakuriu May 29 '14 at 09:02
  • thanks! I've added dizP = {k:val[1:-1].split(',') for k,val in config.items(sezione)} to delete brackets but this way it keeps adding '' quotes to the elements, when you write, then read it back from the file:i.e.: lista[1]= 10 became lista[1]='10', then again lista[1]=''10'' – user2239318 May 29 '14 at 12:03
  • 1
    Your question is missing fundamental information. Please provide an example of the file you are trying to parse and the exact result you want to achieve after parsing. You didn't say what kind of list you wanted, so I simply assumed a list of strings. You probably need some other conversion, but you never stated what kind of objects you have in that list. – Bakuriu May 29 '14 at 12:57
  • I need only a numeric list, so I've solved by doing a: dizP['lista1']=map(int, listofelaboratedvalues) – user2239318 May 30 '14 at 13:44

0 Answers0