python 读写文件包含多种编码格式的解决方式
作者:hm11290219 时间:2022-01-12 18:02:10
今天写一个脚本文件,需要将多个文件中的内容汇总到一个txt文件中,由于多个文件有三种不同的编码方式,读写出现错误,先将解决方法记录如下:
# -*- coding: utf-8 -*-
import wave
import pylab as pl
import numpy as np
import pandas as pd
import os
import time
import datetime
import arrow
import chardet
import sys
reload(sys)
sys.setdefaultencoding('utf8')
os.chdir("F:/new_srt")
#get words of srt file
###########################################
def get_word():
path = "F:/new_srt"
filelist = os.listdir(path)
for files in filelist:
print files
encoding = chardet.detect(open(files,'r').read())['encoding']
if encoding == 'utf-8':
data=pd.read_csv(files,encoding="utf-8",sep='\r',header=None)
elif encoding == 'GB2312':
try:
data=pd.read_csv(files,encoding="gbk",sep='\r',header=None)
except UnicodeDecodeError:
data=pd.read_csv(files,encoding="utf-8",sep='\r',header=None)
elif encoding == 'UTF-8-SIG':
data=pd.read_csv(files,encoding="UTF-8-SIG",sep='\r',header=None)
else:
print 'this is an error about %s' % files
data_new=pd.DataFrame(np.reshape(data.values, (-1,3)))
data_new.columns=['index','timecut','content']
filename = os.path.splitext(files)[0] #filetype = os.path.splitext(files)[1]
with open('F:/result.txt', 'a') as file:
file.write(str(filename)+' ' )
for item in data_new['content']:
file.write(item.decode("utf-8") +' ') #s=s.decode("utf-8")
file.write('\n')
if __name__ == '__main__':
get_word()
来源:https://blog.csdn.net/hm11290219/article/details/71426577
标签:python,读写文件,编码格式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
VBScript中LBound函数和UBound函数的用法
2008-06-27 13:02:00
PHP实现批量生成App各种尺寸Logo
2023-07-23 03:59:59
实例讲解如何配置MySQL数据库主从复制
2008-12-17 15:33:00
基于python + django + whoosh + jieba 分词器实现站内检索功能
2023-03-05 05:30:56
![](https://img.aspxhome.com/file/2023/3/82513_0s.png)
使用Python将数组的元素导出到变量中(unpacking)
2022-01-12 10:11:43
Python给定一个句子倒序输出单词以及字母的方法
2022-11-21 00:36:50
python的函数最详解
2022-02-15 17:05:05
在Python中调用ggplot的三种方法
2023-08-23 00:40:58
![](https://img.aspxhome.com/file/2023/7/62567_0s.png)
python机器学习理论与实战(五)支持向量机
2021-11-27 11:36:30
![](https://img.aspxhome.com/file/2023/2/75942_0s.jpg)
Python中装饰器兼容加括号和不加括号的写法详解
2022-08-09 10:46:25
亚马逊购物用户体验分析(三)
2009-10-25 12:53:00
![](https://img.aspxhome.com/file/UploadPic/200910/25/navigational-elements-95s.jpg)
Jsp+Servlet实现简单登录注册查询
2023-07-15 18:08:08
![](https://img.aspxhome.com/file/2023/4/97884_0s.png)
python将天气预报可视化
2021-01-19 01:37:46
![](https://img.aspxhome.com/file/2023/5/110495_0s.jpg)
python第三方异步日志库loguru简介
2022-03-25 14:34:23
![](https://img.aspxhome.com/file/2023/0/115470_0s.png)
Python使用sftp实现传文件夹和文件
2021-09-30 12:27:37
![](https://img.aspxhome.com/file/2023/1/76881_0s.jpg)
Python通过websocket与js客户端通信示例分析
2021-03-23 08:35:49
![](https://img.aspxhome.com/file/2023/4/107674_0s.png)
效控制C#中label输出文字的长度,自动换行
2023-07-22 04:02:49
python实现多进程通信实例分析
2023-04-08 19:44:05
![](https://img.aspxhome.com/file/2023/0/66460_0s.png)
Python实现队列的方法
2023-08-29 06:45:30
新 API 寻求让 JavaScript 操作本地文件
2009-11-27 18:28:00
![](https://img.aspxhome.com/file/UploadPic/200911/27/2g33954gi_lit-43s.gif)