需求:将txt文本中的数据插入到excel中的不同表中,并为每个sheet表添加表头
如果在txt文本中已经有表头,则只需要建立个list列表,将txt名写入列表中,遍历list列表,然后将txt文本写入excel中
因为文件过大,所以这里用了openpyxl,xlwt仅能插入255行,不能满足数据量大的情况
# coding=utf-8
import os
from openpyxl import Workbook
os.chdir('E:/pycharm/Test/profile/')
file_path = 'all.xlsx'#要写入的文件
wb = Workbook()
def write_excel(txt_name,sheet):
f1 = open(txt_name, 'r', encoding='utf-8')
lines = f1.readlines()
for line in lines:
split_line = line.split('\t')
sheet.append(split_line)
app_list = ['brand','cate1','cate2','app_name','uv']
attribute_list = ['category','brand','keyword','uv']
city_list = ['category','brand','city_num','cate2']
keyword_list = ['brand','keyword','uv']
profile_list = ['category','brand','cate1','cate2','uv/pv']
profile_cate1_list = ['category','brand','cate1','uv/pv']
star_list = ['brand','appid','star_name','uv']
join_uv_list = ['category','brand','uv']
sheet = wb.create_sheet('app')
sheet.append(app_list)
write_excel('app.txt',sheet)
sheet = wb.create_sheet('attribute')
sheet.append(attribute_list)
write_excel('attribute.txt',sheet)
sheet = wb.create_sheet('city')
sheet.append(city_list)
write_excel('city.txt',sheet)
sheet = wb.create_sheet('keyword')
sheet.append(keyword_list)
write_excel('keyword.txt',sheet)
sheet = wb.create_sheet('profile')
sheet.append(profile_list)
write_excel('profile.txt',sheet)
sheet = wb.create_sheet('profile_cate1')
sheet.append(profile_cate1_list)
write_excel('profile.txt',sheet)
sheet = wb.create_sheet('star')
sheet.append(star_list)
write_excel('star.txt',sheet)
sheet = wb.create_sheet('join_uv')
sheet.append(join_uv_list)
write_excel('join_uv.txt',sheet)
wb.save(file_path)
下面是xlwt的版本:
#encoding:utf-8
import xlwt
import codecs
import os
def Txt_to_Excel(inputTxt,sheetName,start_row,start_col,outputExcel):
fr = codecs.open(inputTxt,'r',encoding='utf-8')
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet(sheetName)
line_number = 0#记录有多少行,相当于写入excel时的i,
row_excel = start_row
for line in fr:
line_number += 1
row_excel += 1
line = line.strip()
line = line.split('\t')
len_line = len(line)#list中每一行有多少个数,相当于写入excel中的j
col_excel = start_col
for j in range(len_line):
ws.write(row_excel,col_excel,line[j])
col_excel += 1
wb.save(outputExcel)
if __name__=='__main__':
os.chdir(u'E:/pycharm/Test/profile/')
print('开始执行')
sheetName = 'app'#需要写入excel中的Sheet2中,可以自己设定
start_row = 0 #从第0行开始写
start_col = 0 #从第0列开始写
inputfile = 'app.txt' #输入文件
outputExcel = 'excel_result.xls' #输出excel文件
Txt_to_Excel(inputfile,sheetName,start_row,start_col,outputExcel)
print('完成')
sheetName = 'attribute'#需要写入excel中的Sheet2中,可以自己设定
start_row = 0 #从第7行开始写
start_col = 0 #从第3列开始写
inputfile = 'attribute.txt' #输入文件
outputExcel = 'excel_result.xls' #输出excel文件
Txt_to_Excel(inputfile,sheetName,start_row,start_col,outputExcel)
print('完成')
sheetName = 'keyword' # 需要写入excel中的Sheet2中,可以自己设定
start_row = 0 # 从第7行开始写
start_col = 0 # 从第3列开始写
inputfile = 'keyword.txt' # 输入文件
outputExcel = 'excel_result.xls' # 输出excel文件
Txt_to_Excel(inputfile, sheetName, start_row, start_col, outputExcel)
print('完成')
sheetName = 'star'#需要写入excel中的Sheet2中,可以自己设定
start_row = 0 #从第7行开始写
start_col = 0 #从第3列开始写
inputfile = 'star.txt' #输入文件
outputExcel = 'excel_result.xls' #输出excel文件
Txt_to_Excel(inputfile,sheetName,start_row,start_col,outputExcel)
print('完成')