python 修改文件编码方式
import chardet
import os def strJudgeCode(str):
return chardet.detect(str) def readFile(path):
try:
f = open(path, 'r')
filecontent = f.read()
finally:
if f:
f.close() return filecontent def WriteFile(str, path):
try:
f = open(path, 'w')
f.write(str)
finally:
if f:
f.close() def converCode(path):
file_con = readFile(path)
result = strJudgeCode(file_con)
#print(file_con)
if result['encoding'] == 'utf-8':
#os.remove(path)
a_unicode = file_con.decode('utf-8')
gb2312 = a_unicode.encode('gbk')
WriteFile(gb2312, path) def listDirFile(dir):
list = os.listdir(dir)
for line in list:
filepath = os.path.join(dir, line)
if os.path.isdir(filepath):
listDirFile(filepath)
else:
print(line)
converCode(filepath) if __name__ == '__main__':
listDirFile(u'.\TRMD')
详细解释:
1 import chardet
import os def strJudgeCode(str):
return chardet.detect(str)
'''
chardet.detect()返回字典,其中confidence是检测精确度,encoding是编码形式
{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}
()网页编码判断: >>> import urllib
>>> rawdata = urllib.urlopen('http://www.google.cn/').read()
>>> import chardet
>>> chardet.detect(rawdata)
{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}
()文件编码判断 复制代码
import chardet
tt=open('c:\\111.txt','rb')
ff=tt.readline()
#这里试着换成read()也可以,但是换成readlines()后报错
enc=chardet.detect(ff)
print enc['encoding']
tt.close()
''' def readFile(path):
try:
f = open(path, 'r')
filecontent = f.read()
finally:
if f:
f.close() return filecontent def WriteFile(str, path):
try:
f = open(path, 'w')
f.write(str)
finally:
if f:
f.close() def converCode(path):
file_con = readFile(path)
result = strJudgeCode(file_con)
#print(file_con)
if result['encoding'] == 'utf-8':
#os.remove(path)
a_unicode = file_con.decode('utf-8')
'''
使用decode()和encode()来进行解码和编码
u = '中文' #指定字符串类型对象u
str = u.encode('gb2312') #以gb2312编码对u进行编码,获得bytes类型对象str
u1 = str.decode('gb2312')#以gb2312编码对字符串str进行解码,获得字符串类型对象u1
u2 = str.decode('utf-8')#如果以utf-8的编码对str进行解码得到的结果,将无法还原原来的字符串内容
'''
gb2312 = a_unicode.encode('gbk')
WriteFile(gb2312, path) def listDirFile(dir):
list = os.listdir(dir)#返回指定路径下的文件和文件夹列表。
for line in list:
filepath = os.path.join(dir, line)
'''
是在拼接路径的时候用的。举个例子,
os.path.join(“home”, "me", "mywork")
在Linux系统上会返回
“home/me/mywork"
在Windows系统上会返回
"home\me\mywork"
好处是可以根据系统自动选择正确的路径分隔符"/"或"\"
'''
if os.path.isdir(filepath):#os.path.isdir()函数判断某一路径是否为目录
listDirFile(filepath)
else:
print(line)
converCode(filepath) if __name__ == '__main__':
listDirFile(u'.\TRMD')
'''
u'string' 表示 已经是 unicode 编码的 'string' 字符串
# -*- coding: UTF- -*- 这句是告诉python程序中的文本是utf-8编码,让python可以按照utf-8读取程
中文前加u就是告诉python后面的是个unicode编码,存储时按unicode格式存储。
'''
python 修改文件编码方式的更多相关文章
- python批量修改文件内容及文件编码方式的处理
最近公司在做tfs迁移,后面要用新的ip地址去访问tfs 拉取代码 ,所以原来发布脚本中.bat类型的脚本中的的ip地址需要更换 简单说下我们发布脚本层级目录 :每个服务站点下都会有一个发布脚本 . ...
- 格式化MyEclipse代码(java、jsp、js)行的长度@修改java代码字体@修改Properties文件编码方式
每次用MyEclipse/Eclipse自带的快捷键Ctrl+shift+f格式化代码时,如果原来的一行代码大于80列,Eclipse就会自动换为多行.如果想格式化代码后不想让代码换行可以通过以下方式 ...
- Python读取文件编码及内容
Python读取文件编码及内容 最近做一个项目,需要读取文件内容,但是文件的编码方式有可能都不一样.有的使用GBK,有的使用UTF8.所以在不正确读取的时候会出现如下错误: UnicodeDecode ...
- python 修改文件内容
python 修改文件内容 一.修改原文件方式 1 def alter(file,old_str,new_str): 2 """ 3 替换文件中的字符串 4 :param ...
- python 的文件编码处理
python的文件编码处理有点粗鲁 1.不管文件原来是编码类型,读入后都转换成Unicode的编码 2.写入文件时,write函数把变量以读入文件的编码方式写入(根据open(path,mode,en ...
- Ubuntu 查看/修改文件编码
使用enca工具可以查看和修改文件编码 1.安装 sudo apt-get install enca 2.使用 查看文件编码 enca –L zh_CN file_name 修改文件编码 enca – ...
- eclipse修改文件编码
http://topic.csdn.net/u/20080724/14/428de399-790d-442a-8340-3a5fb6dcfcee.html[修改文件编码,假设JS] 在Eclips ...
- 分享一个批量修改文件编码的python脚本
分享一个自己编写的递归查找子目录,将所有cpp文件编码修改为utf-8编码格式的小脚本 #i!/usr/bin/env python3 # -*- coding:utf-8 -*- import os ...
- Python中文件编码的检测
前言: 文件打开的原则是“ 以什么编码格式保存的,就以什么编码格式打开 ”,我们常见的文件一般是以“ utf-8 ”或“ GBK ”编码进行保存的,由于编辑器一般设置了默认的保存和打开方式,所以我们在 ...
随机推荐
- LInux下LD_LIBRARY_PATH的作用与设置
LD_LIBRARY_PATH环境变量主要是用于指定动态链接器(Id)超早elf可执行文件运行时所依赖的动态库(so)的路径,其内容是以冒号分隔的路径列表. Id链接器优先在该变量设置的路径中查找,若 ...
- Kotlin语言学习笔记(7)
反射 // 反射 val c = MyClass::class val c2 = MyClass::class.java // 获取KClass的引用 val widget: Widget = ... ...
- dapper.net 转载
Dapper.NET——轻量ORM Dapper.NET使用 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2 ...
- 重学Java
Java web 的课程告一段落了 现在我觉得我应该重新学习一下 Java基础 先分享下昨天学习递归后写的两个短短的代码 1.求5的阶乘 package test; public class fiv ...
- 求数组中的逆序对的数量----剑指offer36题
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数: 如数组{7,5,6,4},逆序对总共有5对,{7,5},{7,6},{7, ...
- 吴裕雄 python神经网络 水果图片识别(2)
import osimport numpy as npimport matplotlib.pyplot as pltfrom skimage import color,data,transform,i ...
- 通过jquery,从json中读取数据追加到html中
1.下载安装jquery 可通过下面的方法引入在线版本的js: <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jque ...
- hdoj Max Sum Plus Plus(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:----最大M子段和问题给定由 n个整数(可能为负整数)组成的序列a1,a2,a3,……, ...
- 第七章 二叉搜索树(b3)BST:删除
- 数据库事务(Database Transaction)
事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务由事务开始(begin transaction)和事务结束(commit transaction或 ...