鱼C论坛

 找回密码
 立即注册
查看: 1777|回复: 5

[已解决]一个问题代码,好像是编码格式错误,不知道怎么弄,特来求助

[复制链接]
发表于 2017-8-16 10:57:24 | 显示全部楼层 |阅读模式
30鱼币
import os
m = []
x = []

temp = input('请将该代码放在要查找的文件夹内,请输入关键子:')
for each in os.walk('E:/to come again'):
    m.append(each)
y = len(m)
while True:
        if y != (-1):
                for each in m[y-1][2]:
                        x.append(m[y-1][0]+'/'+each)
        else:
                break
        y-=1
for each in x:
        r = open(each)
        for i in r:
                if '曹植' in i:
                        print(i)
                else:
                        print('没找到')
上面是我的代码
下面是我的代码的错误提示:
Traceback (most recent call last):
  File "<pyshell#86>", line 3, in <module>
    for i in r:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 66: illegal multibyte sequence
研究了好久,不知道是不是编码格式错误
最佳答案
2017-8-16 10:57:25
https://pypi.python.org/pypi/chardet

import chardet
#以rb读取文件返回文件的编码(用到了chardet类)
        with open(file_name, 'rb') as f:
            raw = f.read()
            result = chardet.detect(raw)  
            encoding = result['encoding']

        lines = 0   
        with open(file_name,encoding=encoding) as f:
            print('正在分析文件:%s ...' % file_name)     
            try:
                for each_line in f:
                    lines += 1
            except Exception as reason:
                print(str(reason)) # 读取出错显示错误信息......
        print('%s -> %s' % (file_name,lines))
        return lines


import os
import chardet

m = []
x = []

temp = input('请将该代码放在要查找的文件夹内,请输入关键子:')
for each in os.walk('E:/Json60r8'):
    m.append(each)
y = len(m)
while True:
        if y != (-1):
                for each in m[y-1][2]:
                        x.append(m[y-1][0]+'/'+each)
        else:
                break
        y-=1
for each in x:
        #以rb读取文件返回文件的编码(用到了chardet类)
        with open(each, 'rb') as f:
            raw = f.read()
            result = chardet.detect(raw)  
            encoding = result['encoding']
            
        r = open(each, encoding=encoding)
        try:
                for i in r:
                        if '曹植' in i:
                                print(i)
                        else:
                                print('没找到')
        except Exception as reason:
                print(str(reason)) # 读取出错显示错误信息......
       

最佳答案

查看完整内容

https://pypi.python.org/pypi/chardet import chardet #以rb读取文件返回文件的编码(用到了chardet类) with open(file_name, 'rb') as f: raw = f.read() result = chardet.detect(raw) encoding = result['encoding'] lines = 0 with open(file_name,encoding=encoding) as f: print('正在分析文件:%s ...' % file_name) ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-16 10:57:25 | 显示全部楼层    本楼为最佳答案   
https://pypi.python.org/pypi/chardet

import chardet
#以rb读取文件返回文件的编码(用到了chardet类)
        with open(file_name, 'rb') as f:
            raw = f.read()
            result = chardet.detect(raw)  
            encoding = result['encoding']

        lines = 0   
        with open(file_name,encoding=encoding) as f:
            print('正在分析文件:%s ...' % file_name)     
            try:
                for each_line in f:
                    lines += 1
            except Exception as reason:
                print(str(reason)) # 读取出错显示错误信息......
        print('%s -> %s' % (file_name,lines))
        return lines


import os
import chardet

m = []
x = []

temp = input('请将该代码放在要查找的文件夹内,请输入关键子:')
for each in os.walk('E:/Json60r8'):
    m.append(each)
y = len(m)
while True:
        if y != (-1):
                for each in m[y-1][2]:
                        x.append(m[y-1][0]+'/'+each)
        else:
                break
        y-=1
for each in x:
        #以rb读取文件返回文件的编码(用到了chardet类)
        with open(each, 'rb') as f:
            raw = f.read()
            result = chardet.detect(raw)  
            encoding = result['encoding']
            
        r = open(each, encoding=encoding)
        try:
                for i in r:
                        if '曹植' in i:
                                print(i)
                        else:
                                print('没找到')
        except Exception as reason:
                print(str(reason)) # 读取出错显示错误信息......
       

评分

参与人数 1荣誉 +3 鱼币 +3 贡献 +3 收起 理由
忽视 + 3 + 3 + 3 感谢楼主无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-16 11:43:24 | 显示全部楼层
for each in os.walk('E:/to come again',encoding='UTF-8'):
改成这样试试 还有我记得E:后面应该是//
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-16 11:56:13 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-16 12:04:33 | 显示全部楼层
第6行 for each in os.walk('E:/to come again'):应该是路径的斜杠错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-8-26 12:06:16 | 显示全部楼层
ba21 发表于 2017-8-16 10:57
https://pypi.python.org/pypi/chardet

import chardet

好久没登录,现在才上线,不好意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-15 01:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表