求问各路大佬Python的30讲第4题
求问30讲第4题:答案如下:
import os
def print_pos(key_dict):
keys = key_dict.keys()
keys = sorted(keys) # 由于字典是无序的,我们这里对行数进行排序
for each_key in keys:
print('关键字出现在第 %s 行,第 %s 个位置。' % (each_key, str(key_dict)))
def pos_in_line(line, key):
pos = []
begin = line.find(key)
while begin != -1:
pos.append(begin + 1) # 用户的角度是从1开始数
begin = line.find(key, begin+1) # 从下一个位置继续查找
return pos
def search_in_file(file_name, key):
f = open(file_name)
count = 0 # 记录行数
key_dict = dict() # 字典,用户存放key所在具体行数对应具体位置
for each_line in f:
count += 1
if key in each_line:
pos = pos_in_line(each_line, key) # key在每行对应的位置
key_dict = pos
f.close()
return key_dict
def search_files(key, detail):
all_files = os.walk(os.getcwd())
txt_files = []
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_files.append(each_file)
for each_txt_file in txt_files:
key_dict = search_in_file(each_txt_file, key)
if key_dict:
print('================================================================')
print('在文件【%s】中找到关键字【%s】' % (each_txt_file, key))
if detail in ['YES', 'Yes', 'yes']:
print_pos(key_dict)
key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
detail = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' % key)
search_files(key, detail)
但是程序运行之后报错
Traceback (most recent call last):
File "C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/input.py", line 56, in <module>
search_files(key, detail)
File "C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/input.py", line 46, in search_files
key_dict = search_in_file(each_txt_file, key)
File "C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/input.py", line 25, in search_in_file
for each_line in f:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 8132: illegal multibyte sequence
请问这个是什么原因呢?
打开文件使用
f = open(file_name,encoding='utf-8')
打开文件编码问题 dlnb526 发表于 2020-4-4 17:59
打开文件使用
打开文件编码问题
谢谢大佬!
Traceback (most recent call last):
File "C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/input.py", line 56, in <module>
search_files(key, detail)
File "C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/input.py", line 46, in search_files
key_dict = search_in_file(each_txt_file, key)
File "C:/Users/Lenovo/AppData/Local/Programs/Python/Python37-32/input.py", line 25, in search_in_file
for each_line in f:
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python37-32\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa6 in position 0: invalid start byte
但是添加之后又出现了这样的报错,不知道有没有办法解决? emmm你打开的是什么文件呀,你需要自己查一下它的编码格式,打开Notepad++或者VScode之类的文本编辑器,在右下角可以看到 dlnb526 发表于 2020-4-4 18:06
emmm你打开的是什么文件呀,你需要自己查一下它的编码格式,打开Notepad++或者VScode之类的文本编辑器,在 ...
dlnb526 发表于 2020-4-4 18:06
emmm你打开的是什么文件呀,你需要自己查一下它的编码格式,打开Notepad++或者VScode之类的文本编辑器,在 ...
所以这个只适用于全部都是文本文件的情况下才可以使用,是吗?如果有.exe,.dll等就不可以使用了? 余敏丰 发表于 2020-4-4 18:11
所以这个只适用于全部都是文本文件的情况下才可以使用,是吗?如果有.exe,.dll等就不可以使用了?
www.jianshu.com/p/7797946c71e3参考这个文章哦 dlnb526 发表于 2020-4-4 18:18
www.jianshu.com/p/7797946c71e3参考这个文章哦
感谢!
页:
[1]