【问题标题】:Python: How to retain string as is (with '\n') while printing? [duplicate]Python:如何在打印时保留字符串(使用'\n')? [复制]
【发布时间】:2017-05-30 09:29:29
【问题描述】:

我正在从 json url 打印一个字符串。它以不同的行打印。我想按原样打印字符串('\n' 完好无损)。

我的代码:-

TestDocs= urllib.request.urlopen("solr url").read()
TestDocs_obj = json.loads(TestDocs.decode())

Docs = []
for i in TestDocs_obj['response']['docs']:
    matchObj = re.findall(r'open(s)?|education|(?i)sue', i['sentence'])
    if not matchObj:
        Doc = i["sentence"]
        print(Doc)
        Docs.append(Doc)

正在打印:-

微软收购了一家公司。

它位于西班牙。

我想要:-

微软收购了一家公司。\n它位于西班牙

【问题讨论】:

标签: python solr


【解决方案1】:

您可以使用repr转义特殊字符。

s = 'hello\nworld'
print(repr(s)[1:-1])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2020-07-23
    相关资源
    最近更新 更多