python合并工作簿所有内容_使用Python将多个工作簿合并为一个xlsx工作簿
我有4个.csv文件,我每天都在执行争吵。我将解析的csv文件的输出保存为4个单独的.xlsx工作簿。我的目标是将所有4个工作簿合并到一个xlsx工作簿中,每个工作簿都包含在自己的选项卡/工作表中。
我已经读过很多使用openpyxl、xlwt和xlwriter的方法,我只是感到困惑,并寻求专家的指导,使我的具体应用程序的正确方式。。。
我创建的工作簿有4个工作表/选项卡,但当我尝试使用工作表名称写入选项卡时,它似乎覆盖了我以前所做的一切,我就是想不出如何修复它?非常感谢您的帮助和指导!import pandas as pd
import openpyxl
import csv
from openpyxl import Workbook
# this creates an xlsx workbook with 4 worksheets
wb = Workbook()
dest_filename = 'Drop Offenderssssssss.xlsx'
ws = wb.active
ws.title = "DropCount_Offenders"
ws = wb.create_sheet()
ws.title = 'Dropstat_Offenders'
ws = wb.create_sheet()
ws.title = 'DropCountPerSec_Offenders'
ws = wb.create_sheet()
ws.title = 'numPktDrops_Offenders'
wb.save(filename = dest_filename)
# there are 2 possible filenames. this takes user input and stores it as a global date variable to call proper filename
date = str(raw_input("Enter yyyymmdd: "))
date_var = date
# function 1 for tab1 contents:
def dropcount_offenders():
global date_var
filename1 = 'PROBE_HEALTH_GRAPH_Drop_Count_%s-01.01.00.AM.csv' %(date_var)
filename2 = 'PROBE_HEALTH_GRAPH_Drop_Count_%s-01.01.01.AM.csv' %(date_var)
# this trys to open the first possible filename
try:
file_handler = open(filename1)
except:
print"trying the next one"
# if first filename was not found then it locates and opens the 2nd possible filename
try:
file_handler = open(filename2)
except:
print"invalid input"
# this uses pandas library to read the csv contents into memory
data = pd.read_csv(file_handler)
# this renames the columns (takes out spaces)
data.columns = ["Probe_Name", "Recording_Time", "Drop_Count"]
# this defines a filter threshold which clears all rows who's Drop_Count column data = 0
counts = data[data.Drop_Count >= 1].sort_index(by="Probe_Name", ascending=True)
# now I want to append/write my filtered data to a specific tab within the xlsx file
counts.to_excel("Drop Offenderssssssss.xlsx", "DropCount_Offenders")
# function 2 for tab2 contents (overwrites all tabs I previously created and overwrites function 1 as well?):
def dropstat_offenders():
global date_var
filename1 = 'DropStats_%s-01.01.00.AM.csv' %(date_var)
filename2 = 'DropStats_%s-01.01.01.AM.csv' %(date_var)
try:
file_handler = open(filename1)
except:
print"trying the next one"
try:
file_handler = open(filename2)
except:
print"invalid input"
data = pd.read_csv(file_handler)
data.columns = ["Probe_Name", "RecordingTime", "RecordingPeriod", "PrimaryDimension", "BladeId", "dropCount"]
# this removes the columns i dont need to see
del data["RecordingPeriod"]
del data["BladeId"]
drops = data[data.dropCount >= 1].sort_index(by="Probe_Name", ascending=True)
drops.to_excel("Drop Offenderssssssss.xlsx", 'Dropstat_Offenders')
# this runs the above 2 functions in sequence
dropcount_offenders()
dropstat_offenders()
我想要的是dropcount_continers()成为工作簿中的一个选项卡/工作表,dropstat_continers()成为另一个选项卡/工作表,等等。。。?
python合并工作簿所有内容_使用Python将多个工作簿合并为一个xlsx工作簿相关推荐
- python批量提取word指定内容_使用python批量读取word文档并整理关键信息到excel表格的实例...
目标 最近实验室里成立了一个计算机兴趣小组 倡议大家多把自己解决问题的经验记录并分享 就像在CSDN写博客一样 虽然刚刚起步 但考虑到后面此类经验记录的资料会越来越多 所以一开始就要做好模板设计(如下 ...
- 用python爬取qq空间内容_用python爬取QQ空间
原博文 2016-11-18 17:19 − 好久没写博文了,最近捣鼓了一下python,好像有点上瘾了,感觉python比js厉害好多,但是接触不久,只看了<[大家网]Python基础教程(第 ...
- python合并两个文本文件内容_用Python 将两个文件的内容合并成一个新的文件.
一个文件的内容是: Introduction to Programming, Networking Fundamentals, Internetworking Technologies, Platfo ...
- python合并两个文本文件内容_利用python自带插件合并多个excle文件内容
location = "D:/file/" # 你需要合并该目录下excel文件的指定的文件夹 date = "20171016" # 不需要,笔者在这里使用此 ...
- python移动文件中某个内容_如果python中的某些文件类型,则移动文件并创建目录...
这可能是一个简单的问题,但我对 python和编程一般都是新手. 我正在研究一个简单的程序,在镜像源位置的目录结构时,将.mp3文件从一个位置复制/移动到另一个位置.到目前为止我的工作,但它也在目标位 ...
- python正则匹配括号以及内容_【Python】正则表达式匹配最里层括号的内容
现在有一个字符串: str1 = '(subject_id = "A" OR (status_id = "Open" AND (status_id = &quo ...
- python获取藏头诗内容_用Python作诗,生活仍有诗和远方
具体步骤: 使用爬虫爬取全唐诗,总共抓取了71000首. #使用urllib3的内置函数构建爬虫的安全验证,来应对网站的反爬虫机制 http = urllib3.PoolManager( cert_r ...
- python提取txt中指定内容_提取视频中的音频——python三行程序搞定!
写在开头 身处数据爆炸增长的时代,各种各样的数据都飞速增长,视频数据也不例外.我们可以使用 python 来提取视频中的音频,而这仅仅需要安装一个体量很小的python包,然后执行三行程序! ...
- python提取txt中指定内容_提取视频中的音频,Python三行程序搞定!
来源:http://suo.im/5yOtbx 写在开头 身处数据爆炸增长的时代,各种各样的数据都飞速增长,视频数据也不例外.我们可以使用 python 来提取视频中的音频,而这仅仅需要安装一个体量很 ...
- python网络编程内容_图解Python网络编程
Python Python开发 Python语言 图解Python网络编程 本篇索引 (1)基本原理 本篇指的网络编程,仅仅是指如何在两台或多台计算机之间,通过网络收发数据包:而不涉及具体的应用层功能 ...
最新文章
- android用openGl ES绘制任意方程的三维空间图形
- java命令可以但是javac命令找不到
- html表格以pdf格式导出到本地
- 图片文件夹要放在html,在网页中插入图像,若图像文件位于html文件的上两级文件夹,则在文件名之前加入()...
- Java 设计模式之Bridge桥接模式
- #Java编程题-百钱百鸡
- 设置EntityFramework中decimal类型数据精度
- Mac python3.5 + Selenium 开发环境配置
- Android 自动接听来电
- 领域驱动设计营销系统建模
- 广告/友情链接/站内信/会员地址/会员银行卡/我的收藏/评论/搜索/用户标签/用户抬头号/签到/我的足迹/我的优惠卷
- 技术岗网上测评-智力题
- windows,远程开机,远程唤醒(WOL,Wake-on-LAN)
- 关于java WEb怎么调用matlab(二)
- 欧姆龙PLC分频电路
- 微信小程序开发中的这些坑你遇到过吗?
- Java中 9 种常见的 CMS GC 问题分析与解决
- 【小程序从0到1】网络数据请求——request合法域名|GET|POST|跨域?Ajax?
- 线性代数--1.2 行化简与阶梯形矩阵
- 项目中如何避免团队成员相互甩锅?