2

I need a config file to generate my scripts.

[PATHS]
elements = elementA,elementB,elementC,elementD,elementE,elementF,elementG,elementH,elementJ,elementK

As you notice after a point the line gets too long. I can not keep track of the elements written in it and does not look nice at all.

In my script I do the following to read elements:

elements = config["PATHS"]["elements"].split(",")

Is there a nicer way to handle this? I would prefer something that includes linebreak and tabs, for example:

[PATHS]
elements = 
    - elementA
    - elementB
    - elementC
    - elementD
    - elementE
    - elementF
    - elementG
    - elementH
    - elementJ
    - elementK

Any suggestions from the more experienced are welcome too.

I tried splitting with .split("\n\t\t- ") but did not do the work


print("1",config["PATHS"]["elements"])

1 
- elementA
- elementB
- elementC


print("2",config["PATHS"]["elements"].strip())
2 - elementA
- elementB
- elementC


print("3",config["PATHS"]["elements"].strip().split('\n'))
3 ['- elementA', '- elementB', '- elementC']
Kristof Pal
  • 966
  • 4
  • 12
  • 28

1 Answers1

3

This might do what you want:

elements = [
    element.strip(' -')
    for element in config["PATHS"]["elements"].strip().split('\n')]
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • seems to do the job! just out of curiosity, which one of the `strip()` and `split()` is applied first? Want to know what happens internally... – Kristof Pal Mar 03 '16 at 17:35
  • `strip()` removes default whitespaces, `split('\n')` creates a list separating the elements whenever it sees a `\n`, `strip(' -')` removes ` -` from each of those elements – Kristof Pal Mar 03 '16 at 18:08
  • I've always been partial to `s.splitlines()` vs. `s.split('\n')`. `splitlines` is OS-line-ending aware, plus I just find the no-arg function call cleaner-looking. – PaulMcG Mar 04 '16 at 15:28