0

I am printing a string from a json url. It prints it in different lines. I want to print the string as is (with the '\n' intact).

My code:-

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)

it is printing:-

Microsoft buys a company.

It is located in Spain.

I want:-

Microsoft buys a company.\nIt is located in Spain

  • 1
    Question has already been answered here: https://stackoverflow.com/questions/6477823/python-display-special-characters-when-using-print-statement – Jannek S. May 30 '17 at 09:41

1 Answers1

0

You can use reprto escape special characters.

s = 'hello\nworld'
print(repr(s)[1:-1])
gzc
  • 8,180
  • 8
  • 42
  • 62