简体   繁体   中英

How can pathlib read_text() method display German Umlaute correctly on Windows 10 Enterprise?

I have problems reading a text file textFile1 with the following content:

Das erste Mal war noch in der Audition-Phase bei einem Screentest. Sie haben mir das alte Kostüm von einem meiner Vorgänger, Val Kilmer, gegeben. Es war verrückt. Ich weiss noch genau, wie ich es mir angezogen habe und dachte, Batman zu spielen wird hier drinnen unmöglich sein für mich, völlig ausgeschlossen!

from pathlip import Path
p = Path('textFile1')
p.read_text()

The language is German and some of the characters are not displayed correctly:

'Das erste Mal war noch in der Audition-Phase bei einem Screentest.\nSie haben mir das alte Kost ü m von einem meiner Vorg ä nger, Val Kilmer, gegeben.\nEs war verr ü ckt.\nIch weiss noch genau, wie ich es mir angezogen habe und dachte, Batman zu spielen wird hier drinnen unmöglich sein f ü r mich, v ö llig ausgeschlossen!\n'

I came across this problem on another occasion when I tried to import a table into postgreSQL. A workaround was to enter the following from the command line:

chcp 1252
psql -U postgres
SET client_encoding='WIN1252'
\i Path/to/your/.sqlFile

I am working on a Windows machine (Windows 10 Enterprise). Is there a way to solve the problem without changing the client_encoding every time I read text files from the command line? I have done quit a lot of research on this topic but I couldn't find a way to change the settings permanently. It seems to be a "Windows problem", because on my Mac all the characters are displayed correctly.

p.read_text(encoding='UTF-8' )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM