0

I have a following list in my python module:

couples = [("somekey1", "somevalue1"), ("somekey2", "somevalue2"), ("somekey3", "somevalue3"),....]

I am storing configurations for my app in "configs.ini" and i use configparser tor read it. I checked documentation for configparser and didn't find how can i read my list from file.

UPD: Does anybody know how i can read following list from my configs or maybe exists another way to store it in file ?

UPD2: it is list of logins and password.May it can help.

Vetalll
  • 3,472
  • 6
  • 24
  • 34

3 Answers3

3

I'm not sure I understand this correctly but if you want to create a config file to easily read a list like you've shown then create a section in your configs.ini

[section]
key = value
key2 = value2
key3 = value3

and then

>> config = ConfigParser.RawConfigParser()
>> config.read('configs.ini')
>> items = config.items('section')
>> items
[('key', 'value'), ('key2', 'value2'), ('key3', 'value3')]

which is basically what you say you need.

If on the other hand what you are saying is that your config file contains:

[section]
couples = [("somekey1", "somevalue1"), ("somekey2", "somevalue2"), ("somekey3", "somevalue3")]

what you could do is extend the config parser like for example so:

class MyConfigParser(ConfigParser.RawConfigParser):

    def get_list_of_tups(self, section, option):
        value = self.get(section, option)
        import re
        couples = re.finditer('\("([a-z0-9]*)", "([a-z0-9]*)"\)', value)
        return [(c.group(1), c.group(2)) for c in couples]

and then your new parser can get fetch your list for you:

>> my_config = MyConfigParser()
>> my_config.read('example.cfg')
>> couples = my_config.get_list_of_tups('section', 'couples')
>> couples
[('somekey1', 'somevalue1'), ('somekey2', 'somevalue2'), ('somekey3', 'somevalue3')]

The second situation is just making things hard for yourself I think.

jhnwsk
  • 971
  • 12
  • 15
0

You can use the pickle module to dump and load your list to a file.

To dump your list:

import pickle

couples = [("somekey1", "somevalue1"), ("somekey2", "somevalue2"), ("somekey3", "somevalue3"),....]

pickle.dump(couples, open("save.p", "wb"))

To load your list:

couples = pickle.load(open("save.p", "rb"))
jh314
  • 27,144
  • 16
  • 62
  • 82
  • Should it written to file before reading with pickle? Can i save it without serialization, and read with pickle? – Vetalll Aug 06 '13 at 16:04
  • Yes, you should dump your list using pickle, and read from it using pickle. You could also dump your list using two columns, one for key and other for value if you want. – jh314 Aug 06 '13 at 16:15
  • Just make sure to head Pickle Best Practices. As per the documentation, never unpickle data received from an untrusted or unauthenticated source. – Rejected Aug 06 '13 at 16:31
0

What you're looking for is probably the configparser module. Check out these links for more details:

1) How to read and write INI file with Python3?

2) http://docs.python.org/2.7/library/configparser.html#examples

Community
  • 1
  • 1