Naomi
Naomi

Reputation: 1

unicode not printing on when imported from text file python

I am reading in a text file from python and when it prints the Unicode, it does not print the symbol.

input.txt:

hello world
\u00a9

code:

from pathlib import Path 
with open('input.txt','r') as file:
    txt = Path('input.txt').read_text()
    print(txt)

output:

hello world 
\u00a9

expected output:

hello world
°

Upvotes: 0

Views: 148

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 178409

The data printed is exactly what is in the file. To translate Unicode escapes you will need to specify that as an encoding. The original code was also mixing file reading methods, so here's both versions:

with open('input.txt',encoding='unicode-escape') as f:
    txt = f.read()
print(txt)
from pathlib import Path
txt = Path('input.txt').read_text(encoding='unicode-escape')
print(txt)

Output (both versions):

hello world
©

Note that \u00a9 is the copyright symbol, not degree symbol. The degree symbol is \u00b0.

Upvotes: 2

Related Questions