0

python code

import configparser
cfg = configparser.ConfigParser()
cfg.read('config.ini')
message = cfg['common']['msg']
print(message)

config.ini

[common]
msg = hi /n how are you?

output

hi /n how are you?

But I need newline instead of /n printitng

hi
how are you?
Vignesh
  • 1,414
  • 1
  • 9
  • 24
Ansh Ansh
  • 63
  • 5

1 Answers1

0

ConfigParser escapes '\n' in ini values as '\n', so try replacing it.

message = cfg['common']['msg']
message.replace('\\n', '\n')
print(message)
Vignesh
  • 1,414
  • 1
  • 9
  • 24
  • 2
    Other way round. He shoud add the backslash in his config file, not in his code. – wuerfelfreak Dec 29 '20 at 06:39
  • It's not a *good* option, it's the only one... Your proposed code will not work anyway as the escaped character is wrong in the file itself... It's a matter of a simple typo... – Tomerikoo Dec 29 '20 at 08:57