具体是这样,某路径下有很多 excel文件,文件名中包含相同关键字的是一类文件,把包含相同关键字的文件合并成一个文件,生成一个新的csv文件

  

# coding=utf-8
import xlrd
import xlwt
import datetime
import os
import time
import httplib2 # 具体是这样,某路径下有很多 excel文件,文件名中包含相同关键字的是一类文件,把包含相同关键字的文件合并成一个文件,生成一个新的csv文件
class csvFile:
def WriteLog(self, message, fileName):
with open(fileName, 'a') as f:
f.write(message) def combinExcelToCsv(self,keyword_list,mainExceldir = "D:\\work\\Excel_txtProcesss\\new-微博",csvDir="D:\\work\\Excel_txtProcesss\\new-微博-合并"): # 把文件名中包含上述文件列表的文件合并成一个文件,比如最新微博-new77723-长城.xls和最新微博-new60000-长城.xls 合并成一个文件:长城.csv
key_filelist_dict = {}
for root, dirs, files in os.walk(mainExceldir):
for key in keyword_list:
filelist = []
for file in files:
strfilename = mainExceldir + "\\" + file
if file.find(key) > -1:
filelist.append(strfilename)
if len(filelist) > 0:
key_filelist_dict[key] = filelist for key1 in key_filelist_dict.keys():
filename = os.path.join(csvDir, key1 + ".csv")
file_list = key_filelist_dict[key1]
file_index = 0 for file in file_list:
print(file)
try:
data = xlrd.open_workbook(file)
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols # 文件标题
if file_index==0:
rowValues = table.row_values(0)
headtitle = ""
for colIndex in range(0,ncols):
headtitle += str(rowValues[colIndex])+","
self.WriteLog(headtitle, filename) file_index+=1
for i in range(1, nrows):
rowValues = table.row_values(i)
message = ""
for colIndex in range(0, ncols):
message += str(rowValues[colIndex]).replace(",", ",") + ","
print(i)
message = message.replace("\n", "")
message = message.encode("gbk", "ignore").decode("gbk")
self.WriteLog("\n"+message, filename)
except UnicodeDecodeError as rr:
print("error:" + file)
print(rr.args) obj = csvFile()
keyword_list = ["BOSS直聘", "VIVO", "蒙牛", "宝马", "拼多多", "OPPO", "上汽通用", "小米", "优信", "长城", "", "百威", "东风", "东鹏",
"富连网", "链家", "青岛", "一汽大众", "梅赛德斯-奔驰"]
# 存放excel文件路径
mainExceldir="D:\\work\\Excel_txtProcesss\\new-微博"
# 要生成的csv文件路径
csvDir="D:\\work\\Excel_txtProcesss\\new-微博-合并"
obj.combinExcelToCsv(keyword_list,mainExceldir)

python3 把excel文件合并并保存到csv文件的更多相关文章

  1. python爬取当当网的书籍信息并保存到csv文件

    python爬取当当网的书籍信息并保存到csv文件 依赖的库: requests #用来获取页面内容 BeautifulSoup #opython3不能安装BeautifulSoup,但可以安装Bea ...

  2. 使用scrapy爬取的数据保存到CSV文件中,不使用命令

    pipelines.py文件中 import codecs import csv # 保存到CSV文件中 class CsvPipeline(object): def __init__(self): ...

  3. 使用pandas中的raad_html函数爬取TOP500超级计算机表格数据并保存到csv文件和mysql数据库中

    参考链接:https://www.makcyun.top/web_scraping_withpython2.html #!/usr/bin/env python # -*- coding: utf-8 ...

  4. 将一个命令的输出保存到CSV文件

    执行段: 结果段: 补充:配合不同的命令可以使工作更加简单 使用Imort-Csv命令从文件中导入结构化数据

  5. C# 一些代码小结--datGirdView 保存到csv文件

    if (dataGridView1.Rows.Count == 0) { MessageBox.Show("No data available!", "Prompt&qu ...

  6. 多种方法爬取猫眼电影Top100排行榜,保存到csv文件,下载封面图

    参考链接: https://blog.csdn.net/BF02jgtRS00XKtCx/article/details/83663400 https://www.makcyun.top/web_sc ...

  7. 记录python爬取猫眼票房排行榜(带stonefont字体网页),保存到text文件,csv文件和MongoDB数据库中

    猫眼票房排行榜页面显示如下: 注意右边的票房数据显示,爬下来的数据是这样显示的: 网页源代码中是这样显示的: 这是因为网页中使用了某种字体的缘故,分析源代码可知: 亲测可行: 代码中获取的是国内票房榜 ...

  8. iOS开发——数据持久化&本地数据的存储(使用NSCoder将对象保存到.plist文件)

    本地数据的存储(使用NSCoder将对象保存到.plist文件)   下面通过一个例子将联系人数据保存到沙盒的“documents”目录中.(联系人是一个数组集合,内部为自定义对象).   功能如下: ...

  9. np.savetxt()——将array保存到txt文件,并保持原格式

    问题:1.如何将array保存到txt文件中?2.如何将存到txt文件中的数据读出为ndarray类型? 需求:科学计算中,往往需要将运算结果(array类型)保存到本地,以便进行后续的数据分析. 解 ...

随机推荐

  1. eNSP仿真学习和VLAN配置

    路由&交换机基本命令 sys #切换到系统视图(修改配置),Ctrl+Z 返回用户视图 sysname SW1 #设备重命名为SW1 int g0/0/1 #进入接口视图 VLAN配置 首先连 ...

  2. 1015 Reversible Primes (20)(20 point(s))

    problem A reversible prime in any number system is a prime whose "reverse" in that number ...

  3. Failed to resolve: com.android.support:design:25.4.0

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 错误:(27, 13) Failed to resolve: com.android.s ...

  4. play framework系列之打包war

    概览 Play framwork 是我们一直在使用的框架,从刚开始的简单使用,乱起八糟的jar包引用,项目组成员之间的下载项目之后的引用问题等,遇到各种问题,我都一一解决,我将在这个系列中奉上解决方案 ...

  5. Codeforces Round #352 (Div. 1) B. Robin Hood 二分

    B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...

  6. BZOJ 1003 物流运输trans dijstra+dp

    1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3896  Solved: 1608[Submit] ...

  7. Codeforces Round #281 (Div. 2) D. Vasya and Chess 镜面对称 博弈论

    D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Poj 题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  9. SCP用法

    scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port ...

  10. java 程序消耗 cpu 100% 查找方法

    问题原因:由于HashMap是非线程安全的,在多线程访问时,造成死循环. 查找问题方法: 1. top 找出最耗费cpu的进程号 如:27377 2. top -p 27377 -H 找出此进程下的所 ...