[問題] 開啟TXT檔案的問題

看板Python作者 (Zalk)時間4年前 (2020/01/26 17:02), 編輯推噓4(409)
留言13則, 7人參與, 4年前最新討論串1/1
想靠過年學點程式 在udemy 上買了課程 但是照著做後到開啟資料這邊程式出錯 Analysis = "C:\Users\ZalkCheng\Desktop\Data.txt" 會出現 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 好像是\ 的問題 可是換成/ 還是其他都都沒用 請大大幫忙解答一下 感謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.228.184.208 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1580029337.A.AA5.html

01/26 17:08, 4年前 , 1F
""前面加r
01/26 17:08, 1F

01/26 17:09, 4年前 , 2F
不然就是換成\\
01/26 17:09, 2F

01/26 17:20, 4年前 , 3F
前面加r後出現下一行錯誤with open(Analysis, 'r') as csvf
01/26 17:20, 3F

01/26 17:20, 4年前 , 4F
FileNotFoundError: [Errno 2] No such file or directory:
01/26 17:20, 4F

01/26 17:21, 4年前 , 5F
'C:\\Users\\ZalkCheng\\Desktop\\Data.txt'
01/26 17:21, 5F

01/26 18:14, 4年前 , 6F
請 r’C:/.....’
01/26 18:14, 6F

01/27 02:12, 4年前 , 7F
感謝以上
01/27 02:12, 7F

01/27 10:21, 4年前 , 8F
查一下open的語法吧
01/27 10:21, 8F

01/27 16:37, 4年前 , 9F
開啟txt 檔常遇到編碼的問題,通常會在open 裡加上rep
01/27 16:37, 9F

01/27 16:37, 4年前 , 10F
lace ignore,加上 r 後的錯誤訊息為沒在指定位置找到
01/27 16:37, 10F

01/27 16:37, 4年前 , 11F
檔案
01/27 16:37, 11F

01/30 17:32, 4年前 , 12F
應該是你文字檔裡面有中文,中文一個佔兩個字節 可以
01/30 17:32, 12F

01/30 17:32, 4年前 , 13F
用用看.seek() 來重新定位
01/30 17:32, 13F
文章代碼(AID): #1UBLMPgb (Python)