python将数据换为txt的方法有很多,可以用xlrd库实现。本人比较懒,不想按太多用的少的插件,利用已有库pandas将excel文件转换为txt文件。
直接上代码:
'''
function:将excel文件转换为text
author:nstock
date:2018/3/1
'''
import pandas as pd
import re
import codecs
#将excel转化为txt文件
def exceltotxt(excel_dir, txt_dir):
with codecs.open(txt_dir, 'w', 'utf-8') as f:
neg=pd.read_excel(excel_dir, header=none, index=none)
f.write(neg.to_string())
#去除记录行首的数字和空格
def del_linehead_number_speace(orig_txt_dir,saveas_txt_dir):
with open(orig_txt_dir,'r+') as f, open(saveas_txt_dir,'r+') as fw:
lines = f.readlines()
print(len(lines)) #行数
texts = [re.sub(r'(\d)+(\s)+','',lines[num]) for num in range(len(lines))