鱼C论坛

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

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

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

  4. temp = input('请将该代码放在要查找的文件夹内,请输入关键子:')
  5. for each in os.walk('E:/to come again'):
  6.     m.append(each)
  7. y = len(m)
  8. while True:
  9.         if y != (-1):
  10.                 for each in m[y-1][2]:
  11.                         x.append(m[y-1][0]+'/'+each)
  12.         else:
  13.                 break
  14.         y-=1
  15. for each in x:
  16.         r = open(each)
  17.         for i in r:
  18.                 if '曹植' in i:
  19.                         print(i)
  20.                 else:
  21.                         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



  1. import os
  2. import chardet

  3. m = []
  4. x = []

  5. temp = input('请将该代码放在要查找的文件夹内,请输入关键子:')
  6. for each in os.walk('E:/Json60r8'):
  7.     m.append(each)
  8. y = len(m)
  9. while True:
  10.         if y != (-1):
  11.                 for each in m[y-1][2]:
  12.                         x.append(m[y-1][0]+'/'+each)
  13.         else:
  14.                 break
  15.         y-=1
  16. for each in x:
  17.         #以rb读取文件返回文件的编码(用到了chardet类)
  18.         with open(each, 'rb') as f:
  19.             raw = f.read()
  20.             result = chardet.detect(raw)  
  21.             encoding = result['encoding']
  22.             
  23.         r = open(each, encoding=encoding)
  24.         try:
  25.                 for i in r:
  26.                         if '曹植' in i:
  27.                                 print(i)
  28.                         else:
  29.                                 print('没找到')
  30.         except Exception as reason:
  31.                 print(str(reason)) # 读取出错显示错误信息......
  32.       
复制代码

最佳答案

查看完整内容

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



  1. import os
  2. import chardet

  3. m = []
  4. x = []

  5. temp = input('请将该代码放在要查找的文件夹内,请输入关键子:')
  6. for each in os.walk('E:/Json60r8'):
  7.     m.append(each)
  8. y = len(m)
  9. while True:
  10.         if y != (-1):
  11.                 for each in m[y-1][2]:
  12.                         x.append(m[y-1][0]+'/'+each)
  13.         else:
  14.                 break
  15.         y-=1
  16. for each in x:
  17.         #以rb读取文件返回文件的编码(用到了chardet类)
  18.         with open(each, 'rb') as f:
  19.             raw = f.read()
  20.             result = chardet.detect(raw)  
  21.             encoding = result['encoding']
  22.             
  23.         r = open(each, encoding=encoding)
  24.         try:
  25.                 for i in r:
  26.                         if '曹植' in i:
  27.                                 print(i)
  28.                         else:
  29.                                 print('没找到')
  30.         except Exception as reason:
  31.                 print(str(reason)) # 读取出错显示错误信息......
  32.       
复制代码

评分

参与人数 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-4-24 16:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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