python - 在保持时间戳的同时将 XLSX 转换为 CSV

标签 python excel csv time xlsx

我正在尝试将充满 XLSX 文件的目录转换为 CSV。除了我遇到包含时间信息的列的问题外,一切正常。 XLSX 文件是由另一个我无法修改的程序创建的。但我想保持在 Excel 中查看 XLSX 文件时显示的时间与将其转换为 CSV 并在任何文本编辑器中查看时显示的时间相同。

我的代码:

import csv
import xlrd
import os
import fnmatch
import Tkinter, tkFileDialog, tkMessageBox

def main():
    root = Tkinter.Tk()
    root.withdraw()
    print 'Starting .xslx to .csv conversion'
    directory = tkFileDialog.askdirectory()
    for fileName in os.listdir(directory):
        if fnmatch.fnmatch(fileName, '*.xlsx'):
            filePath = os.path.join(directory, fileName)
            saveFile = os.path.splitext(filePath)[0]+".csv"
            savePath = os.path.join(directory, saveFile)
            workbook = xlrd.open_workbook(filePath)
            sheet = workbook.sheet_by_index(0)
            csvOutput = open(savePath, 'wb')
            csvWriter = csv.writer(csvOutput, quoting=csv.QUOTE_ALL)
            for row in xrange(sheet.nrows):
                csvWriter.writerow(sheet.row_values(row))
            csvOutput.close()
    print '.csv conversion complete'

main()

要添加一些细节,如果我在 Excel 中打开一个文件,我会在时间列中看到:

00:10.3
00:14.2
00:16.1
00:20.0
00:22.0

但在我转换为 CSV 后,我在同一位置看到了这个:

0.000118981
0.000164005
0.000186227
0.000231597
0.000254861

感谢 seanmhanson 的回答 https://stackoverflow.com/a/25149562/1858351我能够弄清楚 Excel 将时间转储为一天的小数。虽然我应该尝试更好地学习和使用 xlrd,但为了快速的短期修复,我能够将其转换为秒,然后从秒转换回最初看到的 HH:MM:SS 时间格式。我的(可能是丑陋的)代码如下,以防任何人都可以使用它:

import csv
import xlrd
import os
import fnmatch
from decimal import Decimal
import Tkinter, tkFileDialog

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

def seconds_to_hms(seconds):
    input = Decimal(seconds)
    m, s = divmod(input, 60)
    h, m = divmod(m, 60)
    hm = "%02d:%02d:%02.2f" % (h, m, s)
    return hm

def main():
    root = Tkinter.Tk()
    root.withdraw()
    print 'Starting .xslx to .csv conversion'
    directory = tkFileDialog.askdirectory()
    for fileName in os.listdir(directory):
        if fnmatch.fnmatch(fileName, '*.xlsx'):
            filePath = os.path.join(directory, fileName)
            saveFile = os.path.splitext(filePath)[0]+".csv"
            savePath = os.path.join(directory, saveFile)
            workbook = xlrd.open_workbook(filePath)
            sheet = workbook.sheet_by_index(0)
            csvOutput = open(savePath, 'wb')
            csvWriter = csv.writer(csvOutput, quoting=csv.QUOTE_ALL)
            rowData = []
            for rownum in range(sheet.nrows):
                rows = sheet.row_values(rownum)
                for cell in rows:
                    if is_number(cell):
                        seconds = float(cell)*float(86400)
                        hms = seconds_to_hms(seconds)
                        rowData.append((hms))
                    else:
                        rowData.append((cell))
                csvWriter.writerow(rowData)
                rowData = []
            csvOutput.close()
    print '.csv conversion complete'

main()

最佳答案

Excel 将时间存储为以天为单位的 float 。您将需要使用 XLRD 来确定单元格是否为日期,然后根据需要进行转换。我对 XLRD 不是很好,但你可能想要类似的东西,如果你想保持前导零,则更改字符串格式:

if cell.ctype == xlrd.XL_CELL_DATE:
    try: 
        cell_tuple = xldate_as_tuple(cell, 0)
        return "{hours}:{minutes}:{seconds}".format(
            hours=cell_tuple[3], minutes=cell_tuple[4], seconds=cell_tuple[5])
    except (any exceptions thrown by xldate_as_tuple):
        //exception handling

可在此处找到元组方法文档的 XLRD 日期:https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966#xldate.xldate_as_tuple-function

对于已回答的类似问题,另请参阅此问题:Python: xlrd discerning dates from floats

关于python - 在保持时间戳的同时将 XLSX 转换为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149203/

相关文章:

excel - Amazon Redshift 身份验证问题

excel - 根据另一个单元格中的值更改单元格中的值

function - 如果条件以另一种/更好的方式?

python - 将制表符分隔的文本文件读入 Pandas 数据帧时出现 RunTimeError

python - 如何设置 Pandas DataFrame 左上角单元格的样式

python - python中的文本文件处理

python - 在 PowerShell 中运行 Python?

python - 在打印 “Greeter client received: …”之前,gRPC Python quickstart/helloworld(greeter_client.py)已挂起

python - 如何基于迭代另一个 df 的所有行值来对 pandas 数据框进行子集化?

python - 将多个 numpy 数组保存到一个 csv