循环访问Oracle Cursor并将excel文件另存为字符串变量

时间:2015-04-10 14:58:16

标签: arrays string oracle python-2.7 for-loop

import CX_Oracle
Import xlwt
SQL0 = "SELECT COUNTRY FROM COUNTRYLIST"
cursor0 = con.cursor()
cursor0.execute(SQL0)

for country in cursor0:
    SQL = "SELECT * from TABLE WHERE COUNTRY = %s" % country
    cursor = con.cursor()
    cursor.execute(SQL)
    book = xlwt.Workbook()
    sheet1 = book.add_sheet('Summary')
    for i, row in enumerate(cursor):
        for j, col in enumerate(row):
            sheet1.write(i+1,j,col)  #Starts pasting the data at row 2
    book.save('Output_' + str(country) + '.xls')

这会正确地遍历每个国家/地区,并在每个文件中使用正确的数据,但是,每个excel文件都保存为OUTPUT _(' PERU',)。xls

1 个答案:

答案 0 :(得分:0)

到处都在寻找如何删除字符而无法理解的问题。它认为每次我试图运行它时都会导致错误。我必须把str(country)放在re.sub中才能工作。

countrys = re.sub("[^a-zA-Z]+",'',str(country))
book.save('Output_' + countrys + '.xls')