Python批量处理CSV文件
#encoding: utf-8
__author__ = 'DELL'
import csv
import glob
import datetime
import sys
import os
reload(sys)
#中文错误
sys.setdefaultencoding( "utf-8" )
'''
@author likehua
CSV批处理
'''
class BatchProcessCSV:
def __init__(self,inputfolder="c:\\input\\",outputfolder="c:\\output\\"):
self.inputfolder=inputfolder
self.outputfolder=outputfolder
#批处理
def doBatchAction(self):
startTime=datetime.datetime.now()
print(u"开始处理...")
if (os.path.exists(self.outputfolder)==False):
#pass
os.makedirs(self.outputfolder)
list_dirs = os.walk(self.inputfolder)
for root, dirs, files in list_dirs:
#print i
for file in files:
otput=self.outputfolder+file
self.readcsv2csv(self.inputfolder+file,otput)
print(u"Running.........................\n") endTime=datetime.datetime.now()
print(u"处理完成,耗时:%f秒"%(endTime-startTime).seconds) #读取一个csv提取部分信息生成新的CSV
def readcsv2csv(self,inputfile,outputfile):
with open(inputfile, 'rb') as csvfile:
o=open(outputfile,"wb")
#解决csv浏览乱码问题
o.write('\xEF\xBB\xBF');
writer=csv.writer(o)
#读取列 将字符串转为数组
column=csvfile.readline().split(",")
#print(column.index('App Release Date'))
#print(column)
writer.writerow(['Rank' ,'Category', 'Country ','App Name', 'Value', 'Unit' , 'App Release Date', 'Publisher Name', 'Company Name', 'Parent Company Name'])
reader = csv.reader(csvfile)
#table = reader[0]
#Rank, Category, Store, Device, Type, Country, Period,Version, App_ID, App_Name, Value, Unit, Value_Type, AppURL, App_IAP, App_Category, App_Device, Current_Price, App_Release_Date, Publisher_ID, Publisher_Name, CompanyName, ParentCompanyName, AppNameUnified, AppFranchise, UnifiedAppID, AppFranchiseID, CompanyID, ParentCompanyID
for row in reader:
lenth=len(row)
if lenth>10:
writer.writerow([row[column.index("Rank")],row[column.index("Category")],row[column.index("Country")],row[column.index("App Name")],row[column.index("Value")],row[column.index("Unit")],row[column.index("App Release Date")],row[column.index("Publisher Name")],row[column.index("Company Name")],row[column.index("Parent Company Name")]]) #process
if __name__=="__main__":
csvProcess=BatchProcessCSV("c:\\input\\","e:\\output\\")
csvProcess.doBatchAction()
Python批量处理CSV文件的更多相关文章
- python在处理CSV文件时,字符串和列表写入的区别
概述 Python在处理CSV文件时,如果writerow的对象是<type 'unicode'>字符串时,写入到CSV文件时将会出现一个字符占一个单元格的情况: 但是将字符串转换为列表类 ...
- python批量处理压缩文件
python批量处理压缩文件 博客小序:在数据的处理中,下载的数据很有可能是许多个压缩文件,自己一个一个解压较为麻烦,最近几日自己在处理一次下载的数据时,遇到大量的压缩数据需要处理,于是利用pytho ...
- python批量运行py文件
import os path="E:\\python" #批量的py文件路径 for root,dirs,files in os.walk(path): #进入文件夹目录 for ...
- python中操作csv文件
python中操作csv文件 读取csv improt csv f = csv.reader(open("文件路径","r")) for i in f: pri ...
- Python:使用pymssql批量插入csv文件到数据库测试
并行进程怎么使用? import os import sys import time def processFunc(i): time.sleep(10-i) print i if __name__= ...
- python脚本-excel批量转换为csv文件
pandas和SQL数据分析实战视频教程 https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2& ...
- python批量读取txt文件为DataFrame
我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作.比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢? 首先我们要 ...
- python批量改动指定文件夹文件名称
这小样例仅仅要是说明用python怎么批量改动指定文件夹的文件名称: 记得要把脚本跟改动的文件放在同一个文件夹下 #encoding:utf-8 import os import sys files ...
- Python: 对CSV文件读写 和 Md5加密
1. python 有专门的csv包,直接导入即可. import csv: 2. 直接使用普通文件的open方法 csv_reader=open("e:/python/csv_data/l ...
随机推荐
- 鸟哥的私房菜:Linux磁盘与文件系统原理
1 硬盘物理组成 //原理 磁头负责读写 磁道(硬盘同半径的一圈) 磁柱(所有盘磁道叠加起来的柱) 扇区(2条半径将磁道分开的一个扇形区域,是磁盘的最小存储单位) ------ ...
- HDU 1692 Destroy the Well of Life 水题
Destroy the Well of Life Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showprob ...
- BZOJ 1191: [HNOI2006]超级英雄Hero 匈牙利算法
1191: [HNOI2006]超级英雄Hero Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx Solved: 2xx 题目连接 http:/ ...
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- SMACH专题(一)----安装与初探
最近使用ROS进行任务(Task)执行,深切体会用传统的方法实现是极其繁杂的.比如人脸录入工作,包含人脸检测,识别,语音提示,运动控制,这些子部分基本都是通过订阅话题的回调函数中处理,之间的切换,如人 ...
- [JAVA] JAVA 类路径
Java 类路径 类路径是所有包含类文件的路径的集合. 类路径中的目录和归档文件是搜寻类的起始点. 虚拟机搜寻类 搜寻jre/lib和jre/lib/ext目录中归档文件中所存放的系统类文件 搜寻再从 ...
- USB Mass Storage大容量存储 The Thirteen Class章节的理解
http://blog.csdn.net/xgbing/article/details/7002558 USB Mass Storage 6.7 The Thirteen Class章节的理解 Cas ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- Moq的一些基本用法
本篇体验Moq的一些基本用法.首先通过NuGet安装Moq.包括: 模拟方法的返回值 模拟方法后执行回调函数 模拟方法依次返回多个值 模拟第二次调用方法返回异常 直接返回被模拟方法的原始返回值 模拟泛 ...
- duplicate symbol _main in: / linker command failed with exit code 1
duplicate symbol _main in: /Users/mb467/Library/Developer/Xcode/DerivedData/barChartDemo-gevlnavnpan ...