简体   繁体   English

如何使用Python ConfigParser从配置文件读取换行符

[英]How to read a newline from config file using Python ConfigParser

I have a config File "Email_Properties.cfg" as below 我有一个配置文件“ Email_Properties.cfg”,如下所示

[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 我正在使用ConfigParser读取文件并打印内容,无法使用\\ n获取newLine

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 ? 我如何在配置文件的字符串中获取NewLine? Expected output as below, 预期输出如下

securityDetails
Note : This is an auto generated mail

ConfigParser escapes \\n in ini values as \\\\n , so try with something like ConfigParser将\\n的ini值转义为\\\\n ,因此请尝试使用类似的方法

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. FWIW:问题的示例在Python 2中,但是对于Python> = 3.2的用户: ConfigParser.read_file() 替换ConfigParser.readfp() ,此版本ConfigParser.readfp()起已弃用。 This is not affecting the question/solution though. 但是,这并不影响问题/解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM