Python - печатать читаемый формат из строки, содержащей \ n и другие юникоды? - PullRequest
0 голосов
/ 08 мая 2018

это сообщение изначально было набором, а затем я преобразовал его в строку с 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

как это исправить?

1 Ответ

0 голосов
/ 08 мая 2018

Я думаю, что вы можете конвертировать ваш начальный объект в __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.

...