这条消息最初是一个集合,然后我用str(a)
转换成字符串from astropy.io import fits
import matplotlib.pyplot as plt
image_data = fits.getdata(r"/path/to/image.fits")
plt.imsave("/path/to/image.png", image_data, cmap="gray")
由于某种原因我打印时
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"
它保留所有\ n和\ u2019s并且不将其格式化为新行或\ u2019格式化为“'”右引号..所以它只是以明文显示
print(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("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")
我该如何解决这个问题?
答案 0 :(得分:1)
我认为您可能会将初始对象转换为__repr__
而不是__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
。