求助。。。。
def prtpos(key_dict):keys = key_dict.keys()
keys = sorted(keys)
for each_key in keys:
print('关键字出现在第%s行, 第%s个位置' %(each_key, str(key_dict)))
def find_line(line, key):
pos = []
begin = line.find(key)
while begin != -1:
pos.append(begin +1)
begin = line.find(key, begin+1)
return pos
def search_file(file_name, key):
f = open(file_name, encoding='utf-8')
count = 0
key_dict = dict()
for each_line in f:
count += 1
if key in each_line:
pos = find_line(each_line, key)
key_dict = pos
f.close()
def search_files(key, file_addr):
all_files = os.walk(file_addr)
txt_file = []
for i in all_files:
for each_file in i:
if os.path.splitext(each_file) == '.txt':
each_file = os.path.join(i, each_file)
txt_file.append(each_file)
for each in txt_file:
key_dict = search_file(each, key)
if key_dict:
print('在文件【%s】中找到关键字【%s】'%(each, key))
prtpos(key_dict)
key = input('请输入关键字:')
file_addr = input('请输入地址:')
search_files(key, file_addr)
错误显示:
D:\py----practice\venv\Scripts\python.exe D:/py----practice/11.py
请输入关键字:希望
请输入地址:D://
Traceback (most recent call last):
File "D:/py----practice/11.py", line 86, in <module>
search_files(key, file_addr)
File "D:/py----practice/11.py", line 79, in search_files
key_dict = search_file(each, key)
File "D:/py----practice/11.py", line 58, in search_file
for each_line in f:
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 0xb4 in position 0: invalid start byte
进程已结束,退出代码 1
去掉 encoding = 'utf-8'
def prtpos(key_dict):
keys = key_dict.keys()
keys = sorted(keys)
for each_key in keys:
print('关键字出现在第%s行, 第%s个位置' %(each_key, str(key_dict)))
def find_line(line, key):
pos = []
begin = line.find(key)
while begin != -1:
pos.append(begin +1)
begin = line.find(key, begin+1)
return pos
def search_file(file_name, key):
f = open(file_name)
count = 0
key_dict = dict()
for each_line in f:
count += 1
if key in each_line:
pos = find_line(each_line, key)
key_dict = pos
f.close()
def search_files(key, file_addr):
all_files = os.walk(file_addr)
txt_file = []
for i in all_files:
for each_file in i:
if os.path.splitext(each_file) == '.txt':
each_file = os.path.join(i, each_file)
txt_file.append(each_file)
for each in txt_file:
key_dict = search_file(each, key)
if key_dict:
print('在文件【%s】中找到关键字【%s】'%(each, key))
prtpos(key_dict)
key = input('请输入关键字:')
file_addr = input('请输入地址:')
search_files(key, file_addr) 把encoding = "utf-8"去掉 Twilight6 发表于 2020-5-21 16:18
去掉 encoding = 'utf-8'
D:\py----practice\venv\Scripts\python.exe D:/py----practice/11.py
请输入关键字:希望
请输入地址:D://
Traceback (most recent call last):
File "D:/py----practice/11.py", line 86, in <module>
search_files(key, file_addr)
File "D:/py----practice/11.py", line 79, in search_files
key_dict = search_file(each, key)
File "D:/py----practice/11.py", line 58, in search_file
for each_line in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xd2 in position 19: illegal multibyte sequence
删掉之后的结果是这样的。。想请问一下为什么 XD--Wrf 发表于 2020-5-21 16:47
D:\py----practice\venv\Scripts\python.exe D:/py----practice/11.py
请输入关键字:希望
请输入地址 ...
不要去掉试试,新建个干净点的文件夹,把那个txt文本放文件夹里面,如何地址填这个文件夹的路径试试 Twilight6 发表于 2020-5-21 16:57
不要去掉试试,新建个干净点的文件夹,把那个txt文本放文件夹里面,如何地址填这个文件夹的路径试试
加上之后,放到新文件夹中报出的还是最开始那个错误。。。 qiuyouzhi 发表于 2020-5-21 16:19
把encoding = "utf-8"去掉
去掉是类似gbk编码的错误 XD--Wrf 发表于 2020-5-21 17:16
去掉是类似gbk编码的错误
那你就用记事本改下编码试试
具体百度 qiuyouzhi 发表于 2020-5-21 17:18
那你就用记事本改下编码试试
具体百度
另存为改成utf之后,还是会报出第一个的那种错误
页:
[1]