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中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...
随机推荐
- “error: command 'x86_64-linux-gnu-gcc' failed with exit status 1” in virtualenv
Most of the time these are dependency-issues. Following the stack-trace of the gcc compiler one ca ...
- 关于android屏幕适配的问题(drawable-xxxxxxxx,dp,sp,px等等),偶尔看到了android源代码,关于dpi的区分的值
上一篇博客说了一下.9.png图片http://blog.csdn.net/qq_23195583/article/details/46737419 当然,点九的是指的能够进行拉伸的.那么假设图片不能 ...
- 【JS】一款好用的JS日历选择插件【bootstrap-datetimepicker.js】
1.插件名称:bootstrap-datetimepicker.js,下载地址:上Github下载或者下面链接 2.效果图: 3.使用方法:里面有Demo 链接: https://pan.baidu. ...
- 锋利的jQuery(第二版)源码下载地址
书上给的http://cssrain.sinaapp.com无法访问 问作者邮箱要的: 第1版源码:百度云盘下载:http://pan.baidu.com/share/link?shareid=104 ...
- 基于JavaScript 声明全局变量的三种方式
本文转自脚本之家:http://www.jb51.net/article/36548.htm JS中声明全局变量主要分为显式声明或者隐式声明下面分别介绍. 声明方式一: 使用var(关键字)+变量名( ...
- 系统加速解决方案之Windows XP
系统加速解决方案之Windows XP 在使用Windows XP的过程中,系统速度会随着时间的推移越来越慢,你可重装系统,但重装后,那么多的应用软件也要重新安装,如何在不安装系统的前提下提升Wind ...
- 轻松搞定面试中的“虚"
提要 今天要整理的知识点是C++中有关虚的一切. 包括:虚函数,纯虚函数,虚基类,虚继承... 1.什么是虚函数,有什么作用? 在基类用virtual声明成员函数为虚函数.这样就可以在派生类中重新定义 ...
- Python isalpha() 方法
描述 Python isalpha() 方法检测字符串是否只由字母或汉字组成. 语法 isalpha() 方法语法: S.isalpha() 参数 无. 返回值 如果字符串至少有一个字符并且所有字符都 ...
- STM32F103 AFIO时钟疑问
在stm32F103系列中:AFIO是重映射辅助时钟,如果仅仅是使用第二功能(如uart,spi,),不需要打开,使用第二功能打开GPIO和第二功能时钟.我反复测试是这样的 AFIO时钟由RCC_AP ...
- python 中 with 用法
前言 with 语句适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源, 比如文件使用后自动关闭/线程中锁的自动获取和释放等. 问题引出 如下代码: fil ...