看板 Python 關於我們 聯絡資訊
各位高手,小的最近在練習寫python讀寫檔案的程式 http://www.cmlab.csie.ntu.edu.tw/~left/test/ 用的是3.2板的 但一直出現下面的error UnicodeDecodeError: 'cp950' codec can't decode bytes in position 349-350: illegal multibyte sequence 程式如下 是否可以請大大幫忙一下要怎麼改呢? #-*- coding:utf-8 -*- import glob import os def remove(filename): s=filename.split("\\") try: with open(filename) as fin, open("./output//"+s[1],"w") as fout: for each_line in fin: if each_line.find("^ 返回文章列表")!= -1: break for each_line in fin: if each_line.find("你現在看到的首頁 Layout")!= -1: break else: print(each_line,end="",file=fout) except IOError as ioerr: print('File error:'+ str(ioerr)) #except UnicodeDecodeError: # pass cwd=os.getcwd() os.chdir(cwd) if not os.path.exists("output"): os.mkdir("output") files = glob.glob('./test//*') for file in files: remove(file) -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.175.211 ※ 編輯: left 來自: 140.112.175.211 (06/10 20:50) ※ 編輯: left 來自: 140.112.175.211 (06/10 21:21)
freelancer:你的第一行告訴python你的檔用是用utf-8 encode的 06/10 21:44
freelancer:看一下你editor 的設定,確定真的是這樣 06/10 21:45
left:感謝你的回答 我用的是python官網上的編輯器 06/10 22:30
left:http://www.python.org/getit/ 06/10 22:31
left:Python 3.2.3 Windows x86 MSI Installer 這要從哪邊改啊? 06/10 22:31
KSJ:簡單說是編碼不是cp950 可是你卻用cp950去解它 06/11 17:24
left:好像是 在這個地方就錯了 for each_line in fin: 06/11 18:01
left:是不是因為 each_line有混一些不是 cp950的code造成的阿 06/11 18:03
left:如果是 這一般要怎麼解決阿? 06/11 18:03
buganini:open(..., encoding="...") 06/11 19:55
left:encoding這邊是要用哪一種方式阿 06/11 20:00
left:可以了 感謝 buganini 06/12 01:36