尝试读取xlrd,提取数据和编写csv

时间:2015-07-25 13:31:42

标签: python excel csv xlrd

我正在尝试读取excel文件,提取一些数据,然后将其写为csv。这对我来说很新鲜,而且我搞砸了某个地方:我不断得到一个空的csv。我确定我错过了一些非常基本的东西,但是如果我能看到的话,我会感到很沮丧。这是代码: ``` 导入xlrd 进口口 导入csv 来自zipfile导入ZipFile 导入日期时间

datafile = "./2013_ERCOT_Hourly_Load_Data.xls"
outfile = "./2013_Max_Loads.csv"


def parse_file(datafile):

   workbook = xlrd.open_workbook(datafile)
   sheet = workbook.sheet_by_index(0)
   data = None
   outputlist = []

   for col in range(1, sheet.ncols):

        cv = sheet.col_values(col, start_rowx=1, end_rowx=None)

        header = sheet.cell_value(0,col)

        maxval = max(cv)

        maxpos = cv.index(maxval) + 1

        maxtime = sheet.cell_value(maxpos, 0)

        realtime = xlrd.xldate_as_tuple(maxtime, 0)

        year = realtime[0]

        month = realtime[1]

        day = realtime[2]

        hour = realtime[3]

        data = [
            'Region:', header,
            'Year:', year,
            'Month:', month,
            'Day:', day,
            'Hour:', hour,
             maxpos,
             maxtime,
            realtime,
            maxval,
            ]

    path = "./2013_Max_Loads.csv"
    return outputlist



def save_file(data, filename):

    with open(filename, "wb") as f:
        writer = csv.writer(f, delimiter='|')
        for line in data:
            writer.writerow(line)

parse_file(datafile)

save_file(parse_file(datafile),"2013_Max_Loads.csv")

1 个答案:

答案 0 :(得分:0)

您声明outfile但不使用

您没有传递要保存的文件的目录(路径)。

我还认为两次调用parse_file可能会搞砸你。只需传递文件名并从save_file函数中调用它。

我还发现您将output list作为空白列表返回。

所以在这里,试试这个。我将假设您的xlrd命令是正确的,因为我没有亲自使用该模块。

import csv

import xlrd


def parse_file(datafile):

   workbook = xlrd.open_workbook(datafile)
   sheet = workbook.sheet_by_index(0)
   outputlist = []
   outputlist_append = outputlist.append

   for col in range(1, sheet.ncols):
        cv = sheet.col_values(col, start_rowx=1, end_rowx=None)
        header = sheet.cell_value(0,col)
        maxval = max(cv)
        maxpos = cv.index(maxval) + 1
        maxtime = sheet.cell_value(maxpos, 0)
        realtime = xlrd.xldate_as_tuple(maxtime, 0)
        year = realtime[0]
        month = realtime[1]
        day = realtime[2]
        hour = realtime[3]

        data = [
            'Region:', header,
            'Year:', year,
            'Month:', month,
            'Day:', day,
            'Hour:', hour,
             maxpos,
             maxtime,
            realtime,
            maxval,
            ]
        outputlist_append(data)

    return outputlist


def save_file(data, filename):

    parse_file(data)
    with open(filename, 'wb') as f:
        writer = csv.writer(f, delimiter='|')
        for line in data:
            writer.writerow(line)
    return


datafile = "./2013_ERCOT_Hourly_Load_Data.xls"
outfile = "./2013_Max_Loads.csv"

save_file(datafile, outfile)
  

更新:在功能save_file()的代码中进行编辑,以实现@ wwii的建议。

尝试替换下面的新save_file()

def save_file(data, filename):

    parse_file(data)
    with open(filename, 'wb') as f:
        wr = csv.writer(f, delimiter='|')
        wr.writerows(data)
    return

此外,将变量(您使用writer)更改为wr之类的内容。你真的希望避免与一个与你正在调用的方法,函数或类同名的变量发生任何可能的冲突。