4

In my config file I have something like that :

[Section_1]
List=Column1,Column2,Column3,Column4

Now, I would like to process it in my main file as normal lists :

    config = configparser.ConfigParser()
    config.read("configTab.ini")
    for index in range(len(List)):
                 sql=sql.replace(List[index],"replace("+List[index]+","'hidden'")")

Now when I read from configuration file "List" is a normal String. What is the best approach to to it?

If I put a normal list variable in my main code it this way:

List=['Column1','Column2','Column3','Column4']

Then it works fine, but I would like to get that from my configure file,

Thanks

bazyl
  • 263
  • 1
  • 7
  • 17
  • Does this answer your question? [Getting a list from a config file with ConfigParser](https://stackoverflow.com/questions/6759016/getting-a-list-from-a-config-file-with-configparser) – Toni Penya-Alba Oct 22 '20 at 14:24

3 Answers3

8

Use str.split:

List = List.split(',')

string = 'a, b, c'
print(string.split(','))
>> ['a', 'b', 'c']
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

The answer by @DeepSpace is not entirely correct. The leading whitespace around 'b' and 'c' is included in the output if one executes this line as written (e.g. 'b' is actually ' b').

To avoid leading and trailing whitespace, try:

string = 'a, b, c'
print([i.strip() for i in string.split(',')])
>> ['a', 'b', 'c']
Brian Pollack
  • 198
  • 3
  • 12
0

Use Regular expression to remove extra space and split with commas re.py

pattern = re.compile("^\s+|\s*,\s*|\s+$")
str_list = 'a, b, c'
cis_list = pattern.split(str_list)
>> ['a', 'b', 'c']