作者left ()
看板Python
标题有关字串读写的问题
时间Sun Jun 10 20:49:40 2012
各位高手,小的最近在练习写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)
1F:推 freelancer:你的第一行告诉python你的档用是用utf-8 encode的 06/10 21:44
2F:→ freelancer:看一下你editor 的设定,确定真的是这样 06/10 21:45
3F:→ left:感谢你的回答 我用的是python官网上的编辑器 06/10 22:30
5F:→ left:Python 3.2.3 Windows x86 MSI Installer 这要从哪边改啊? 06/10 22:31
6F:→ KSJ:简单说是编码不是cp950 可是你却用cp950去解它 06/11 17:24
7F:→ left:好像是 在这个地方就错了 for each_line in fin: 06/11 18:01
8F:→ left:是不是因为 each_line有混一些不是 cp950的code造成的阿 06/11 18:03
9F:→ left:如果是 这一般要怎麽解决阿? 06/11 18:03
10F:→ buganini:open(..., encoding="...") 06/11 19:55
11F:→ left:encoding这边是要用哪一种方式阿 06/11 20:00
12F:→ left:可以了 感谢 buganini 06/12 01:36