Python打開文件報錯SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape


#path 是文件路徑

f=open('C:\Users\jingqiu\Desktop\New Text Document.txt')

結果報錯SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

文件路徑加 r 變成 f=open(r'C:\Users\jingqiu\Desktop\New Text Document.txt')

或者用f=open('C:\\Users\\jingqiu\\Desktop\\New Text Document.txt')

然后f.read()結果是。。。。。。。。。

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    f.read()
  File "C:\Users\xiuwe\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 17: character maps to <undefined>

編碼問題

f = open(r'C:\Users\jingqiu\Desktop\New Text Document.txt',encoding='utf-8')

f.read()

OK ^_^

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM