Python / Excel - 切片提取的Excel数据 - 排除行维护结构

时间:2014-10-02 19:45:46

标签: python excel xlrd

所以我试图在数据提取过程中排除前三行。

for col_num in xrange(sheet.ncols):
    col = sheet.col_values(col_num, start_rowx=3, end_rowx=None)
    writer.writerow(col) #this syntax also may be skewing my results as well

这个for循环消除了前3行放置然后将行转换为列。

关于如何维护数据结构但同时消除行的任何建议?

以下完整脚本:

import glob
import os
import xlrd
import csv

ROOTDIR = r'C:\Users\username\Desktop\Mults'
wb_pattern = os.path.join(ROOTDIR, '*.xlsx')

workbooks = glob.glob(wb_pattern)

with open('merged.csv', 'wb') as outcsv:
writer = csv.writer(outcsv)
    for wb in workbooks:
        book_path = os.path.join(ROOTDIR, wb)
        book =  xlrd.open_workbook(book_path)
        sheet = book.sheet_by_index(0)
        for colx in xrange(sheet.ncols):
            col = sheet.col_values(colx, start_rowx=2, end_rowx=None)
            writer.writerow(col) #this syntax also may be skewing my results

谢谢!

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

如果您想要行值,为什么要将列拉为行?拉出行值并写下:

import glob
import os
import xlrd
import csv

ROOTDIR = r'C:\Users\username\Desktop\Mults'
wb_pattern = os.path.join(ROOTDIR, '*.xlsx')

workbooks = glob.glob(wb_pattern)
start_rownum = 3 # or wherever you want to start copying
with open('merged.csv', 'wb') as outcsv:
    writer = csv.writer(outcsv)
    for wb in workbooks:
        book_path = os.path.join(ROOTDIR, wb)
        book =  xlrd.open_workbook(book_path)
        sheet = book.sheet_by_index(0)
        for rownum in xrange(start_rownum, sheet.numrows):
            row = sheet.row_values(rownum)
            writer.writerow(row)