#!/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的更多相关文章

  1. Python读取Json字典写入Excel表格的方法

    需求: 因需要将一json文件中大量的信息填入一固定格式的Excel表格,单纯的复制粘贴肯定也能完成,但是想偷懒一下,于是借助Python解决问题. 环境: Windows7 +Python2.7 + ...

  2. Python 文本(txt) 转换成 EXCEL(xls)

    #!/bin/env python # -*- encoding: utf-8 -*- #------------------------------------------------------- ...

  3. python取mysql数据写入excel

    环境:MySQLdb openpyxl模块 python去zabbix的mysql数据库中取交换机不同时间段的进出口流量,然后写入excel中,每天cron执行,每周四邮件发送.(代码中第一行必须加上 ...

  4. python 创建txt并且写入做追加

    import os def create_str_to_txt(self,date,str_data): """ 创建txt,并且写入 """ ...

  5. Python xlrd xlwt 读取写入Excel.

    import xlrd import xlwt #读取 xlrd.Book.encoding = "gbk" wb = xlrd.open_workbook(filename='s ...

  6. python讲一个列表写入excel表中

    连接为http://blog.csdn.net/a491057947/article/details/47614263 http://www.crifan.com/export_data_to_exc ...

  7. Python将MySQL表数据写入excel

    背景:将mysql表查询结果写入excel. 1.使用sqlyog工具将查询结果导出到Excel.xml中,用excel打开发现:因为text字段中有回车换行操作,显示结果行是乱的. 2.用mysql ...

  8. Python爬虫学习(二) ——————爬取前程无忧招聘信息并写入excel

    作为一名Pythoner,相信大家对Python的就业前景或多或少会有一些关注.索性我们就写一个爬虫去获取一些我们需要的信息,今天我们要爬取的是前程无忧!说干就干!进入到前程无忧的官网,输入关键字&q ...

  9. 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)

    前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...

随机推荐

  1. JS将数字转换为大写汉字人民币

    <script language="jscript"> function convertCurrency(currencyDigits) { // Constants: ...

  2. Python之对象的属性

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之对象的属性 #http://python.jobbole.com/82622/ #对象的属性 ...

  3. 对UserDict的研究

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #对UserDict的研究 class UserDict(): def __init__(self, dict ...

  4. 拯救者14ISK添加ssd6记录

    说起为何,我要安装ssd,拯救者14isk配置也不低,我加了4Gddr4的内存,目前8G内存ddr4/2G独显ddr5显卡GT960/i5-6300处理器.每次开机响应慢,还会出现磁盘100%,很受不 ...

  5. ORA-04089: 无法对 SYS 拥有的对象创建触发器

    ORA-04089: 无法对 SYS 拥有的对象创建触发器 http://bbs.csdn.net/topics/390355220 猛然发现自己是用sys用户创建表空间,创建用户,赋权,顺手就把sq ...

  6. Oracle实例

    ORA-12505 Listener refused the connection with following error:ORA-12505,TNS:listener 确定这是连接数据库的SID错 ...

  7. Ulipad Python输入先后输出问题

    print "Enter a interger"number=input() 在菜单栏 python-----设置参数----在Parameters:那栏加个参数 -u , 就可以 ...

  8. 转 虫师的selenium借助AutoIt识别上传(下载)详解

    selenium借助AutoIt识别上传(下载)详解 2014-12-27 11:26 by 虫师, 755 阅读, 1 评论, 收藏,  编辑 AutoIt目前最新是v3版本,这是一个使用类似BAS ...

  9. CURL实现HTTP的GET POST方法

    Curl是Linux下一个非常强大的http命令行工具,其功能十分强大. 一.CURL对HTTP的常规訪问 1. 訪问站点 $ curl http://www.linuxidc.com 回车之后.ww ...

  10. 取出分组后每组的第一条记录(不用group by)按时间排序

    --操作日志表 CREATE TABLE [dbo].[JobLog]( [JobLogId] [int] IDENTITY(1,1) NOT NULL, [FunctionId] [nvarchar ...