2013-08-05 38 views
0

我想一个xls文件转换为CSV和使用一个名称output加上今天的日期和时间保存。转换工作,但我遇到了文件名称的问题。 所需输出中的文件名应为:output day time.csv,例如output 2013-08-05 10:50:55.csv尝试的日期和时间添加到输出文件名(Python)的

这是我的尝试。 name打印我的东西,因为我想要的,但是当我把它放在open() funcion不工作。

name = 'output '+str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+'.csv' 
archivo_csv = open(str(name), 'wb') 

这是整个功能:

import xlrd 
import csv 

def xls_to_csv: 
    print "Saving from xls to csv" 
    wb = xlrd.open_workbook('example.xls') 
    sh = wb.sheet_by_name('Page1') 

    // This two lines are the ones that they are giving me problems 
    name = 'output '+str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+'.csv' 
    archivo_csv = open(str(name), 'wb') 

    wr = csv.writer(archivo_csv, quoting=csv.QUOTE_ALL) 
    for rownum in xrange(sh.nrows): 
      wr.writerow(sh.row_values(rownum)) 
    archivo_csv.close() 

这是错误表示:
IOError: [Errno 22] invalid mode ('wb') or filename: 'output 2013-08-05 10:59:44.csv'

预先感谢。

+0

任何显示错误? – Raptor

+0

是的。 'IOError:[Errno 22] invalid mode('wb')or filename:'output 2013-08-05 10:59:44.csv'' –

+0

而你的操作系统是?也许你对文件名允许的限制。 –

回答

2

,如果你使用的是Windows那么它的命名规则问题,在windows你不能保持 字符,如在名称:/\ > * ? " | ..

尽量保持命名约定一样

name = 'output '+str(datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S"))+'.csv' 
相关问题