简体   繁体   English

如何使用 configparser 文件键值获取新行?

[英]How can I get new line with configparser file key values?

python code python代码

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 output

hi /n how are you?

But I need newline instead of /n printitng但我需要换行符而不是 /n printitng

hi
how are you?

ConfigParser escapes '\n' in ini values as '\n', so try replacing it. ConfigParser 将 ini 值中的 '\n' 转义为 '\n',因此请尝试替换它。

message = cfg['common']['msg']
message.replace('\\n', '\n')
print(message)

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

相关问题 如何修补ConfigParser键/值? - How can I patch a ConfigParser key/value? 按下某个键后如何写入文件然后进入新行 - How can i write to a File after a Certain Key is Pressed and then get to a new Line 如何通过读取.txt文件的每一行来将键值添加到字典中并更新值? - How can I add key values into a dictionary from reading in each line of a .txt file and update values? 如何使用configparser对象获取配置文件路径 - How to get configuration file path with configparser object 我有一个txt文件。 如何获取字典键值并打印出现在其中的文本行? - I have a txt file. How can I take dictionary key values and print the line of text they appear in? Python configparser 从一个部分获取所有内容并写入新文件 - Python configparser get all from a section and write to new file ConfigParser获取给定值的密钥 - ConfigParser Get Key on given Value 从python中的configparser读取时,如何将缩进包含在多行值中? - How can I get indentation to be included in a multiline value when read from configparser in python? 如何使用坐标系获取文件中的特定字符,而不计算换行或第一行 - How can I use a coordinate system to get to specific characters in a file, not counting new lines or the first line 如何使用python从json文件中的键获取数组值? - How can I get array values from key in json file with python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM