0
我寫在Python一個簡單的代碼只有打開和讀取文件UTF-8誤差與蟒open()函數
def read_text():
quotes = open("C:/Users/Matteo/Desktop/quotes.txt")
contents_of_file = quotes.read()
print(contents_of_file)
quotes.close()
read_text()
當我嘗試執行它,這是什麼似乎
Traceback (most recent call last):
File "C:\Python27\read.py", line 6, in <module>
read_text()
File "C:\Python27\read.py", line 2, in read_text
quotes = open("‪C:/Users/Matteo/Desktop/quotes.txt")
IOError: [Errno 22] invalid mode ('r') or filename: '\xe2\x80\xaaC:/Users /Matteo/Desktop/quotes.txt'
在互聯網上搜索我瞭解到,問題在於IDLE識別出C,\ xe2 \ x80 \ xaa之前的Unicode字符,即「左對右嵌入」。我不知道這是什麼以及如何從我的代碼中刪除。
在源代碼文件的開頭是否有'# - * - coding:utf-8 - * - '(或其他編碼)行?我認爲它應該是'utf-8' –
@AndrewCherevatkin IDLE問我要添加那個字符串,我做了但沒有任何改變 –
@melpomene哦,我做了它,現在它的工作原理,謝謝,我該如何標記你的評論爲回答? –