2

I have a config File "Email_Properties.cfg" as below

[email_Properties]
Subject=Security keywords Scan Info
Body=securityDetails\n Note : This is an auto generated mail

I am using ConfigParser to read file and print the content, Not able to get newLine using \n

config = ConfigParser.ConfigParser()
config.readfp(open(r'Email_Properties.cfg'))
body=config.get('email_Properties', 'Body')
print body

output:

securityDetails\n Note : This is an auto generated mail

How i can get NewLine present in the string of Config file ? Expected output as below,

securityDetails
Note : This is an auto generated mail
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Sunil Kumar
  • 367
  • 2
  • 5
  • 19

1 Answers1

4

ConfigParser escapes \n in ini values as \\n, so try with something like

body.replace('\\n', '\n')

FWIW: The question's example is in Python 2, but for users of Python >=3.2: ConfigParser.read_file() replaces the ConfigParser.readfp() which as of then is deprecated. This is not affecting the question/solution though.

Thomas Fauskanger
  • 2,536
  • 1
  • 27
  • 42