python合并同类型excel表格的方法

本文实例为大家分享了python合并同类型excel表格的具体代码,供大家参考,具体内容如下

python脚本如下,验证有效。

#!/usr/bin/env python

# -*- coding: UTF-8 -*-
import os, csv


class CSVTopoIreator:
  def __init__(self, filename):
    self.infile = open(filename, 'rb')
    self.reader = csv.reader(self.infile)
  def __iter__(self):
    return self
  def next(self):
    try:
      row = self.reader.next()
    except StopIteration:
      self.infile.close()
      raise StopIteration
    return row


def main():
csvfile = file('csv_test.csv', 'wb')
writer = csv.writer(csvfile,delimiter=',',dialect='excel')
writer.writerow(['Items', 'Measure', 'Result'])
for (dirpath, dirnames, filenames) in os.walk('.'):
for name in filenames:
if name != "new1.py" or name != "niniubi.csv":
filename = dirpath + '/' + name
print "csv filename:", filename
ireator = CSVTopoIreator(filename)
#writer.writerow(ireator)
for a in ireator:
print a
writer.writerow(a)
csvfile.close()

if __name__ == '__main__':
  main()

合并当前文件夹里csv文件到一个excel表里,筛选需要的数据,excel2007里面alt+;,

复制所选内容,把筛选的数据复制到新的标签页里,可进行均值分析。

相关推荐