我怎么能逃脱序列?

时间:2016-01-19 10:14:00

标签: python python-3.x

当我加载文件时,'\ n'自动转换为'\\ n'。 我可以获得原始字符串吗?或者你有什么想法吗?

setting.yaml:

before : '\n'
after  : ''

源:

with open(r"C:\setting.yaml", 'r', encoding='utf-8') as text:
    print(text)
    raw_yml = yaml.load(text)
    before = raw_yml.get("before")  # '\\n'
    after = raw_yml.get("after")    # ''

    sample_str = "\n\n\txxxx\n\n"
    result = sample_str.replace(before, after)

    # I want to get "\txxxx", but "\n\n\txxxx\n\n" was printed.
    print(result)

0 个答案:

没有答案