将excel文件转换为csv的Python程序,在excel的日期列中发布

将excel文件转换为csv的Python程序,在excel的日期列中发布,python,Python,我是python新手,使用下面的代码将excel文件转换为csv 代码是 但excel中的列具有以下值 Case Code Date Amount 5428165773 UA02 4/23/2014 $(1,626.00) 显示为 'Case','Code','Date','Amount' '5428165773','UA02',,'41752.0','-1626.0' 我也试着加上这个,但没用 dialect='excel', quotechar="'"

我是python新手,使用下面的代码将excel文件转换为csv

代码是
但excel中的列具有以下值

Case    Code    Date    Amount
5428165773  UA02    4/23/2014    $(1,626.00)
显示为

'Case','Code','Date','Amount'
'5428165773','UA02',,'41752.0','-1626.0'
我也试着加上这个,但没用

dialect='excel', quotechar="'"

Excel使用浮点数表示自固定日期起的天数。您可以使用
datetime
模块计算日期并创建字符串

import datetime

exceldate = datetime.date(1899, 12, 30)

d = exceldate + datetime.timedelta(days=41752)

print d
日期时间。日期(2014年4月23日)


如果您已经安装了熊猫模块, 这些代码将读取excel文件并存储为dataframe

import pandas as pd
xls = read_excel('path_to_file.xls')
然后

将数据帧写入CSV

你可以在


我认为以下函数是您所需要的,它还处理datetime.time:

def xldate_to_python_date(value):
    """
    convert xl date to python date
    """
    date_tuple = xlrd.xldate_as_tuple(value, 0)
    ret = None
    if date_tuple == (0, 0, 0, 0, 0, 0):
        ret = datetime.datetime(1900, 1, 1, 0, 0, 0)
    elif date_tuple[0:3] == (0, 0, 0):
        ret = datetime.time(date_tuple[3],
                            date_tuple[4],
                            date_tuple[5])
    elif date_tuple[3:6] == (0, 0, 0):
        ret = datetime.date(date_tuple[0],
                            date_tuple[1],
                            date_tuple[2])
    return ret
这是您的文档。上述函数引用自

顺便说一下,如果您使用my library
pyexcel
,您的csv\u from\u excel函数可以重写如下:

import pyexcel

def csv_from_excel(file1):
    excel_file = pyexcel.Reader(file1)
    csv_file = pyexcel.Writer("test.csv")
    csv_file.write_reader(excel_file)
    csv_file.close()

更多文档可在年、月、日、时、分、秒=xlrd.xldate\u as\u元组(条目,0)上找到

这解决了我的问题

感谢大家,首先感谢Alex在第一次尝试中提供了非常有用的指导

更新: 我的代码现在看起来是这样的,但我一直在写行级输出

#!/bin/env python
import xlrd
import csv
from os import sys

def csv_from_excel(file1):
    workbook = xlrd.open_workbook(file1)
    worksheet = workbook.sheet_by_name('sheet1')
    csv1 = open('test.csv', 'wb')
    wr = csv.writer(csv1,quoting=csv.QUOTE_ALL)
    for rownum in xrange(worksheet.nrows):
        if rownum > 2:
            i=0
            for entry in worksheet.row_values(rownum):
                i=i+1
                if i==3:
                    yr, mnth, dy, hr, min, sec =xlrd.xldate_as_tuple(entry, 0)
                    print str(mnth)+'/'+str(dy)+'/'+str(yr)
                    #wr.writerow(str(mnth)+'/'+str(dy)+'/'+str(yr))
                else:
                    print entry
                    #wr.writerow(unicode(entry).encode("utf-8"))
    your_csv_file.close()

if __name__ == "__main__":
    csv_from_excel(sys.argv[1])
电流输出 5428165773 UA02 4/23/2014 -1626.0

你会明白我需要以上的输出 5428165773,UA02,4/23/2014,-1626.0

请评论

更新: 使用print-in for循环(而不是writerow)也解决了这个问题,因为它需要整行


谢谢

查看xldate作为元组函数。这可能有助于将Excel的“自1900年以来的天数”转化为更有意义的内容。这个Stackoverflow帖子可能会有帮助:不是一个复制品(因为它是Java),而是和这里一样的问题(Excel将日期存储为数字)谢谢Alex,我正在尝试用use-xldate\u as\u tuple函数重写我的代码谢谢chfw我尝试安装pyexcel,然后执行建议的代码导入pyexcel文件“/usr/lib/python2.6/site-packages/pyexcel-0.0.5-py2.6.egg/pyexcel/_init____;.py.py”,第14行,在from.readers导入Reader,BookReader,书籍文件中“/usr/lib/python2.6/site-packages/pyexcel-0.0.5-py2.6.egg/pyexcel/readers.py”,第10行,在from.iterators导入sheetrator文件中”/usr/lib/python2.6/site-packages/pyexcel-0.0.5-py2.6.egg/pyexcel/iterators.py“,第10行,在导入六个导入中:没有名为six@itsavy,尝试pip install six以获取丢失的模块。我稍后会查一查为什么遗漏了六个。setup.py中指定了6作为依赖项。您是使用pip安装pyexcel还是克隆它并从源代码安装?您有哪个版本的setuptools?
def xldate_to_python_date(value):
    """
    convert xl date to python date
    """
    date_tuple = xlrd.xldate_as_tuple(value, 0)
    ret = None
    if date_tuple == (0, 0, 0, 0, 0, 0):
        ret = datetime.datetime(1900, 1, 1, 0, 0, 0)
    elif date_tuple[0:3] == (0, 0, 0):
        ret = datetime.time(date_tuple[3],
                            date_tuple[4],
                            date_tuple[5])
    elif date_tuple[3:6] == (0, 0, 0):
        ret = datetime.date(date_tuple[0],
                            date_tuple[1],
                            date_tuple[2])
    return ret
import pyexcel

def csv_from_excel(file1):
    excel_file = pyexcel.Reader(file1)
    csv_file = pyexcel.Writer("test.csv")
    csv_file.write_reader(excel_file)
    csv_file.close()
#!/bin/env python
import xlrd
import csv
from os import sys

def csv_from_excel(file1):
    workbook = xlrd.open_workbook(file1)
    worksheet = workbook.sheet_by_name('sheet1')
    csv1 = open('test.csv', 'wb')
    wr = csv.writer(csv1,quoting=csv.QUOTE_ALL)
    for rownum in xrange(worksheet.nrows):
        if rownum > 2:
            i=0
            for entry in worksheet.row_values(rownum):
                i=i+1
                if i==3:
                    yr, mnth, dy, hr, min, sec =xlrd.xldate_as_tuple(entry, 0)
                    print str(mnth)+'/'+str(dy)+'/'+str(yr)
                    #wr.writerow(str(mnth)+'/'+str(dy)+'/'+str(yr))
                else:
                    print entry
                    #wr.writerow(unicode(entry).encode("utf-8"))
    your_csv_file.close()

if __name__ == "__main__":
    csv_from_excel(sys.argv[1])