Python 模块chardet安装过程(windows环境)
2013-12-18 17:16
656 查看
最近需要一个txt文件的批量转码功能,在网上找到一段批量处理java源文件的py程序如下:
View Code
其中import了python的chardet模块,此模块需要单独安装,到http://pypi.python.org/pypi/chardet#downloads下载chardet-2.1.1.tar.gz,
解压后其中有setup.py文件,用于模块的安装,执行命令:python setup.py install ,报错:ImportError: No module named setuptools ,
需要安装setup tools,网上下载ez_setup.py,cmd执行之。重跑一遍setup.py,chardet安装成功!
另:ubuntu安装命令: # apt-get install python-setuptools
#-*- coding: utf-8 -*- import codecs import os import shutil import re import chardet def convert_encoding(filename, target_encoding): # Backup the origin file. shutil.copyfile(filename, filename + '.bak') # convert file from the source encoding to target encoding content = codecs.open(filename, 'r').read() source_encoding = chardet.detect(content)['encoding'] print source_encoding, filename content = content.decode(source_encoding) #.encode(source_encoding) codecs.open(filename, 'w', encoding=target_encoding).write(content) def main(): for root, dirs, files in os.walk(os.getcwd()): for f in files: if f.lower().endswith('.txt'): filename = os.path.join(root, f) try: convert_encoding(filename, 'utf-8') except Exception, e: print filename def process_bak_files(action='restore'): for root, dirs, files in os.walk(os.getcwd()): for f in files: if f.lower().endswith('.txt.bak'): source = os.path.join(root, f) target = os.path.join(root, re.sub('\.txt\.bak$', '.txt', f, flags=re.IGNORECASE)) try: if action == 'restore': shutil.move(source, target) elif action == 'clear': os.remove(source) except Exception, e: print source if __name__ == '__main__': # process_bak_files(action='clear') main()
View Code
其中import了python的chardet模块,此模块需要单独安装,到http://pypi.python.org/pypi/chardet#downloads下载chardet-2.1.1.tar.gz,
解压后其中有setup.py文件,用于模块的安装,执行命令:python setup.py install ,报错:ImportError: No module named setuptools ,
需要安装setup tools,网上下载ez_setup.py,cmd执行之。重跑一遍setup.py,chardet安装成功!
另:ubuntu安装命令: # apt-get install python-setuptools
相关文章推荐
- python操作三大主流数据库(1)python操作mysql①windows环境中安装python操作mysql数据库的MySQLdb模块mysql-client
- 在Windows环境下为Python 2.5安装SSL模块
- windows环境下安装Python的redis扩展模块
- python安装第三方模块(windows环境)
- windows环境下安装python的mysqldb模块
- windows的python开发环境下如何安装其他模块(httplib2)
- libsvm学习-1 libsvm-3.21在Python-3.4.4, Pycharm, windows环境下的安装过程
- windows环境安装python-igraph全过程
- 4000 python3.6 windows环境下paramiko模块的安装和使用
- windows环境下安装python模块大招
- FFMPEG在windows平台编译的详细过程,包括环境安装
- windows平台python 2.7环境编译安装zbarlight
- windows下安装python模块
- windows下安装python及django的开发环境
- windows安装python-ssh访问模块(paramiko)
- Windows环境下安装Python2.x环境搭建
- Windows环境下python2.7安装mysql-python
- Python Windows环境安装配置,以及Eclipse+Pydev开发
- Windows10下Python安装与环境配置