python 将txt内容写入excel
#!/usr/bin/env python
# -*- coding: utf-8 -* import os
import xlrd
import xlwt
import datetime
import re
from utils.XUtils import XUtils def create_and_write_data_to_excel(p_file_name_prefix=None, p_sheetname=None, p_data=None):
wbk = xlwt.Workbook()
if p_sheetname is None:
p_sheetname = 'Sheet1'
sheet = wbk.add_sheet(p_sheetname, cell_overwrite_ok=True)
for i in xrange(len(p_data)):
for j in xrange(len(p_data[i])):
sheet.write(i, j, p_data[i][j])
filename = p_file_name_prefix + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".xls"
wbk.save(filename)
return wbk if __name__ == '__main__':
module_path = os.path.dirname(__file__)
filename = module_path + '/report_db_20180301_20180302_11881.2_0_100_gdt.txt'
"""
txt 内容
gdt 20180301 -1.0000 6 2 0.3333 -0.1667 -0.5000
gdt 20180302 -1.0000 4 1 0.2500 -0.2500 -1.0000
gdt SUM 11881.2000 10 3 0.3000 1188.1200 3960.4000
all_acc=10, new_acc=3, r=0.3000 unit_price_all=1188.120000 unit_price_new=3960.400000
#新建一个总的列表
按照每行读取txt,按照空格分割成一个小的列表,append到大的列表,后进行写入excel
""" fopen2 = open(filename, 'r')
lines = fopen2.readlines()
all_list = []
excel_title = [u'广点通',u'日期',u'收入',u'总激活数',u'新增激活数',u'新增激活数/总激活数',u'总激活单价',u'新增激活单价']
all_list.append(excel_title)
for line in lines:
# charts to list
l1 = re.split('[ ]+', line)
all_list.append(l1) create_and_write_data_to_excel(p_file_name_prefix='txt_to_excel',p_sheetname='test1',p_data=all_list)
print('success')
python 将txt内容写入excel的更多相关文章
- Python读取Json字典写入Excel表格的方法
需求: 因需要将一json文件中大量的信息填入一固定格式的Excel表格,单纯的复制粘贴肯定也能完成,但是想偷懒一下,于是借助Python解决问题. 环境: Windows7 +Python2.7 + ...
- Python 文本(txt) 转换成 EXCEL(xls)
#!/bin/env python # -*- encoding: utf-8 -*- #------------------------------------------------------- ...
- python取mysql数据写入excel
环境:MySQLdb openpyxl模块 python去zabbix的mysql数据库中取交换机不同时间段的进出口流量,然后写入excel中,每天cron执行,每周四邮件发送.(代码中第一行必须加上 ...
- python 创建txt并且写入做追加
import os def create_str_to_txt(self,date,str_data): """ 创建txt,并且写入 """ ...
- Python xlrd xlwt 读取写入Excel.
import xlrd import xlwt #读取 xlrd.Book.encoding = "gbk" wb = xlrd.open_workbook(filename='s ...
- python讲一个列表写入excel表中
连接为http://blog.csdn.net/a491057947/article/details/47614263 http://www.crifan.com/export_data_to_exc ...
- Python将MySQL表数据写入excel
背景:将mysql表查询结果写入excel. 1.使用sqlyog工具将查询结果导出到Excel.xml中,用excel打开发现:因为text字段中有回车换行操作,显示结果行是乱的. 2.用mysql ...
- Python爬虫学习(二) ——————爬取前程无忧招聘信息并写入excel
作为一名Pythoner,相信大家对Python的就业前景或多或少会有一些关注.索性我们就写一个爬虫去获取一些我们需要的信息,今天我们要爬取的是前程无忧!说干就干!进入到前程无忧的官网,输入关键字&q ...
- 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)
前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...
随机推荐
- POJ 1579 Function Run Fun 记忆化递归
典型的记忆化递归问题. 这类问题的记忆主要是利用数组记忆.那么已经计算过的值就能够直接返回.不须要进一步递归了. 注意:下标越界.递归顺序不能错,及时推断是否已经计算过值了,不要多递归. 或者直接使用 ...
- 8、java内部类
一.基本介绍 内部类是指在一个外部类的内部再定义一个类.类名不需要和文件夹相同. 内部类可以是静态static的,也可用public,default,protected和private修饰:而外部顶级 ...
- 插入UUID,出现Data truncation: Data too long for column 'id' at row 1
ssc.udf.register("getuuid", () => UUID.randomUUID().toString) val stuPCountDF_tmp1=ssc. ...
- 使用Nginx限制同一IP的访问频率
http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html http://nginx.org/en/docs/http/ngx_http ...
- 微信小程序-自定义底部导航
代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
- HDUOJ----John
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- Loading...加载图收集
收集来源:http://cs.fangjia.com/zoushi/
- 自定义Microsoft Visual Studio 代码模板,增加公司和个人信息
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp目录里面有各种新建模板分类: 修 ...
- ios 利用UIScrollView实现图片缩放
前言:我们希望做出的效果是,实现图片的分页滑动,且每张图片都可以双击或用手指捏合来缩放大小.上一篇讲解UIGestureRecognizer的中,我们介绍的UIPinchGestureRecogniz ...
- mongodb win7 32位系统安装以及配置
今天安装 win7 32位系统 mongodb 费了好大劲..记录一下,希望相同的同学可以少踩点坑. 1.安装 我安装的是3.2.4 地址:http://downloads.mongodb.org/ ...