新手/遇到了一个看不懂的异常
Traceback (most recent call last):File "D:/python学习专栏/文本关键字查找.py", line 10, in <module>
for each_line in file1:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xc0 in position 16: illegal multibyte sequence
本题是Python30课的作业4,原程序如下:
import os
initial=input('请输入待查找的目录:')
key=input('请输入关键字:')
for(root,dirs,file)in os.walk(initial):
for each in file:
count_line=0
y=1
z=0
if os.path.splitext(each)=='.txt':
file1=open(os.path.join(root,each))
for each_line in file1:
count_line+=1
count_index=[]
x=0
for i in range(len(each_line)):
if each_line==key:
count_index.append(i)
x=z=1
if x:
if y:
print('在文件【{0}】中找到关键字【{1}】'.format(os.path.join(root,each),key))
y=0
print('关键字出现在第{0}行,第{1}个位置。'.format(count_line,count_index))
if z:
print('=================================================')
想请教一下错误原因,及如何解决~
编码问题
改成import os
initial=input('请输入待查找的目录:')
key=input('请输入关键字:')
for(root,dirs,file)in os.walk(initial):
for each in file:
count_line=0
y=1
z=0
if os.path.splitext(each)=='.txt':
file1=open(os.path.join(root,each),encoding='utf-8')
for each_line in file1:
count_line+=1
count_index=[]
x=0
for i in range(len(each_line)):
if each_line==key:
count_index.append(i)
x=z=1
if x:
if y:
print('在文件【{0}】中找到关键字【{1}】'.format(os.path.join(root,each),key))
y=0
print('关键字出现在第{0}行,第{1}个位置。'.format(count_line,count_index))
if z:
print('=================================================') 编码问题,这样应该可以了:
import os
initial=input('请输入待查找的目录:')
key=input('请输入关键字:')
for(root,dirs,file)in os.walk(initial):
for each in file:
count_line=0
y=1
z=0
if os.path.splitext(each)=='.txt':
try:
file1=open(os.path.join(root,each))
except Exception:
file1 = open(os.path.join(root, each), encoding='utf-8')
for each_line in file1:
count_line+=1
count_index=[]
x=0
for i in range(len(each_line)):
if each_line==key:
count_index.append(i)
x=z=1
if x:
if y:
print('在文件【{0}】中找到关键字【{1}】'.format(os.path.join(root,each),key))
y=0
print('关键字出现在第{0}行,第{1}个位置。'.format(count_line,count_index))
if z:
print('=================================================') zltzlt 发表于 2020-7-27 22:33
编码问题,这样应该可以了:
但是还是报错呀……和原来一样的错误 1q23w31 发表于 2020-7-27 22:24
编码问题
改成
我之前就尝试过这么改了……但是还是报错
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 2: invalid start byte
(不过错误好像不一样了) 天下稽首 发表于 2020-7-28 08:31
我之前就尝试过这么改了……但是还是报错
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb i ...
试试用二进制读取,然后再把它decode成utf-8,
类似于这样
https://img-blog.csdn.net/20180416194054338?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIzOTUzNzE3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 天下稽首 发表于 2020-7-28 08:31
我之前就尝试过这么改了……但是还是报错
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb i ...
使用 rb 打开试试? liuzhengyuan 发表于 2020-7-28 08:50
使用 rb 打开试试?
错的……好像更离谱了?
Traceback (most recent call last):
File "D:/python学习专栏/文本关键字查找(网络测试版2).py", line 10, in <module>
file1=open(os.path.join(root,each),'rb',encoding='utf-8')
ValueError: binary mode doesn't take an encoding argument 天下稽首 发表于 2020-7-28 08:56
错的……好像更离谱了?
Traceback (most recent call last):
File "D:/python学习专栏/文本关键字查 ...
把 encoding 去掉试试? 1q23w31 发表于 2020-7-28 08:36
试试用二进制读取,然后再把它decode成utf-8,
类似于这样
不会decode函数哎……而且你刚刚那个附件一直加载不出来/捂脸 本帖最后由 1q23w31 于 2020-7-28 09:16 编辑
天下稽首 发表于 2020-7-28 09:07
不会decode函数哎……而且你刚刚那个附件一直加载不出来/捂脸
图片出不来吗?
import os
initial=input('请输入待查找的目录:')
key=input('请输入关键字:')
for(root,dirs,file)in os.walk(initial):
for each in file:
count_line=0
y=1
z=0
if os.path.splitext(each)=='.txt':
try:
file1=open(os.path.join(root,each))
except Exception:
file1 = open(os.path.join(root, each), 'rb')
lines = file1.read().decode('utf-8')
for each_line in lines:
count_line+=1
count_index=[]
x=0
for i in range(len(each_line)):
if each_line==key:
count_index.append(i)
x=z=1
if x:
if y:
print('在文件【{0}】中找到关键字【{1}】'.format(os.path.join(root,each),key))
y=0
print('关键字出现在第{0}行,第{1}个位置。'.format(count_line,count_index))
if z:
print('=================================================') 1q23w31 发表于 2020-7-28 09:08
图片出不来吗?
嗯,加载不出来 天下稽首 发表于 2020-7-28 09:10
嗯,加载不出来
发到上面了 liuzhengyuan 发表于 2020-7-28 08:59
把 encoding 去掉试试?
去掉之后程序都跑不起来/捂脸 1q23w31 发表于 2020-7-28 09:08
图片出不来吗?
首先,在try那个位置没有定义lines
然后我这样改了一下,
try:
file1=open(os.path.join(root,each))
lines=file1.read()
然后……错误就更多了……
Traceback (most recent call last):
File "D:/python学习专栏/文本关键字查找(网络测试版3).py", line 12, in <module>
lines=file1.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xc0 in position 16: illegal multibyte sequence
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/python学习专栏/文本关键字查找(网络测试版3).py", line 15, in <module>
lines = file1.read().decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 16: invalid start byte 本帖最后由 1q23w31 于 2020-7-28 09:29 编辑
天下稽首 发表于 2020-7-28 09:21
首先,在try那个位置没有定义lines
然后我这样改了一下,
try:
指定模式打开试试
import os
initial=input('请输入待查找的目录:')
key=input('请输入关键字:')
for(root,dirs,file)in os.walk(initial):
for each in file:
count_line=0
y=1
z=0
if os.path.splitext(each)=='.txt':
file1=open(os.path.join(root,each),'r',encoding='utf-8')
for each_line in file1:
count_line+=1
count_index=[]
x=0
for i in range(len(each_line)):
if each_line==key:
count_index.append(i)
x=z=1
if x:
if y:
print('在文件【{0}】中找到关键字【{1}】'.format(os.path.join(root,each),key))
y=0
print('关键字出现在第{0}行,第{1}个位置。'.format(count_line,count_index))
if z:
print('=================================================')
1q23w31 发表于 2020-7-28 09:25
指定模式打开试试
依然是这个错误
Traceback (most recent call last):
File "D:/python学习专栏/文本关键字查找(网络测试版4).py", line 11, in <module>
for each_line in file1:
File "D:\Python软件\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 2: invalid start byte 天下稽首 发表于 2020-7-28 09:27
依然是这个错误
Traceback (most recent call last):
File "D:/python学习专栏/文本关键字查找(网络 ...
你这是什么文件 天下稽首 发表于 2020-7-28 09:21
首先,在try那个位置没有定义lines
然后我这样改了一下,
try:
你没指定二进制打开{:9_219:} 1q23w31 发表于 2020-7-28 09:34
你这是什么文件
你是指?报错的那个吗?我也不是很清楚啊,看错误提示能找出来吗?
页:
[1]
2