python - Python - 从包含 \n 和其他 unicode 的字符串打印可读格式?
问题描述
此消息最初是一个集合,然后我用 str(a) 转换为字符串
a = "Let\u2019s trade!\n\u00a0\n\nAn Old Friendship, A New Day!\nHere comes the old, visiting at your home.\nIt comes with a new story, about how to live the present, about how in his past he did wrong.\n\nThe new day shines andx2"
出于某种原因,当我打印它时
print(a)
它保留所有 \n 和 \u2019s 并且不会将其格式化为新行或将 \u2019 格式化为“'”右引号..所以它只是以纯文本形式显示
Let\u2019s trade!\n\u00a0\n\nAn Old Friendship, A New Day!\nHere comes the old, visiting at your home.\nIt comes with a new story, about how to live the present, about how in his past he did wrong.\n\nThe new day shines andx2
通常如果我这样做
print("Let\u2019s trade!\n\u00a0\n\nAn Old Friendship, A New Day!\nHere comes the old, visiting at your home.\nIt comes with a new story, about how to live the present, about how in his past he did wrong.\n\nThe new day shines andx2")
它将输出为
Let’s trade!
An Old Friendship, A New Day!
Here comes the old, visiting at your home.
It comes with a new story, about how to live the present, about how in his
past he did wrong.
The new day shines and
我该如何解决?
解决方案
我认为您可能会将初始对象转换为 a__repr__
而不是 a __str__
。
差异看起来像您正在经历的:
Python 3.6.5 (default, Mar 30 2018, 06:41:53)
>>> a = "Let\u2019s trade!\n\u00a0\n\nAn Old Friendship, A New Day!\nHere comes the old, visiting at your home.\nIt comes with a new story, about how to live the present, about how in his past he did wrong.\n\nThe new day shines andx2"
>>> print(a)
Let’s trade!
An Old Friendship, A New Day!
Here comes the old, visiting at your home.
It comes with a new story, about how to live the present, about how in
his past he did wrong.
The new day shines andx2
>>> a_repr = repr(a)
>>> a_repr
"'Let’s trade!\\n\\xa0\\n\\nAn Old Friendship, A New Day!\\nHere comes the old, visiting at your home.\\nIt comes with a new story, about how to live the present, about how in his past he did wrong.\\n\\nThe new day shines andx2'"
>>> print(a_repr)
'Let’s trade!\n\xa0\n\nAn Old Friendship, A New Day!\nHere comes the old, visiting at your home.\nIt comes with a new story, about how to live the present, about how in his past he did wrong.\n\nThe new day shines andx2'
我会调查你是如何得到这个字符串的,并确保底层调用是str
,而不是repr
。
推荐阅读
- redis - Redis 服务崩溃并显示“无法打开 RDB 文件 systemdd(在服务器根目录 /etc/cron.d 中)进行保存:权限被拒绝”
- reactjs - 从 parcel v1 -> CRA 切换,如何在开发期间通过重新加载来提供静态 Web 文件
- python - 如何在python中重新连接mongdb?
- php - 输入字段中的日期时间值
- javascript - 在jQuery中上传图像后如何在输入字段中显示图像文件名
- python - 动画情节“列表”对象没有属性“set_data”,即使我遵循针对其他类似问题的建议?
- api - 产品的 BigCommerce 和 Myob API 集成
- docker - 是否可以附加到正在运行的 docker build 命令
- android - 有谁知道与 Android 兼容的 Unity 2018 轮廓着色器?
- html - mouseleave 被触发