阅读xls,将所有日期转换为正确的格式, - >写信给csv

时间:2015-02-19 14:50:59

标签: python csv import-from-excel

我正在读取excel文件并将其写为csv。一些列包含在excel中格式化为浮点数的日期。在写入CSV之前,所有这些字段都需要转换为正确的日期时间(dd / mm / YY)。 我发现了一些关于它如何工作的好文章,但是努力让它同时在打开的工作表中为所有行工作。 (Python中的新手)

现在代码如下所示:

wb = xlrd.open_workbook(args.inname)
    xl_sheet = wb.sheet_by_index(0)
    print args.inname
    print ('Retrieved worksheet: %s' % xl_sheet.name)
    print outname

    # TODO: Convert xldate.datetime from the date fileds to propper datetime

    output = open(outname, 'wb')
    wr = csv.writer(output, quoting=csv.QUOTE_ALL)

    for rownum in xrange(wb.sheet_by_index(0).nrows):
        wr.writerow(wb.sheet_by_index(0).row_values(rownum))

    output.close()

我确定我必须更改“for rownum ....”行,但我正在努力做到这一点。我尝试了几个选项,都失败了。

感谢

2 个答案:

答案 0 :(得分:3)

在将其写入文件,转换值之前,您需要遍历该行。您确定它在for rownum线附近是正确的:

# You need to know which columns are dates before hand
# you can't get this from the "type" of the cell as they 
# are just like any other number

date_cols = [5,16,23]

... # Your existing setup code here #

# write the header row (in response to OP comment)
headerrow = wb.sheet_by_index(0).row_values(0)
wr.writerow(headerrow)

# convert and write the data rows (note range now starts from 1, not 0)
for rownum in xrange(1,wb.sheet_by_index(0).nrows):
    # Get the cell values and then convert the relevant ones before writing
    cell_values = wb.sheet_by_index(0).row_values(rownum)
    for col in date_cols:
        cell_values[col] = excel_time_to_string(cell_values[col])

    wr.writerow(cell_values)

您在excel_time_to_string()函数中的确切内容取决于您 - @MarkRansom的答案有合理的方法 - 或者您可以使用xlrd自己的软件包版本in this answer. < / p>

例如:

def excel_time_to_string(xltimeinput):
    return str(xlrd.xldate.xldate_as_datetime(xltimeinput, wb.datemode))

*编辑*

在尝试后回复评论中的帮助请求。这是excel_time_to_string()

的更加防错版本
def excel_time_to_string(xltimeinput):
    try:
        retVal = xlrd.xldate.xldate_as_datetime(xltimeinput, wb.datemode)
    except ValueError:
        print('You passed in an argument in that can not be translated to a datetime.')
        print('Will return original value and carry on')
        retVal = xltimeinput

    return retVal

答案 1 :(得分:2)

从Excel到Python的转换非常简单:

>>> excel_time = 42054.441953
>>> datetime.datetime(1899,12,30) + datetime.timedelta(days=excel_time)
datetime.datetime(2015, 2, 19, 10, 36, 24, 739200)

或者完全转换为字符串:

def excel_time_to_string(excel_time, fmt='%Y-%m-%d %H:%M:%S'):
    dt = datetime.datetime(1899,12,30) + datetime.timedelta(days=excel_time)
    return dt.strftime(fmt)

>>> excel_time_to_string(42054.441953)
'2015-02-19 10:36:24'
>>> excel_time_to_string(42054.441953, '%d/%m/%y')
'19/02/15'