Python xlsxwriter TypeError:元组索引必须是整数,而不是str

时间:2014-12-09 20:57:51

标签: python python-2.7 csv xlsxwriter

我是xlsxwriter模块的新手,我收到一个我无法弄清楚的错误。我在Ubuntu linux机器和Python版本2.7.3上运行以下代码:

import xlsxwriter
import csv
from csv import DictReader, DictWriter
import sys
import time

csvfile = sys.argv[1]

recFields = ['Recommendation1', 'Recommendation2', 'Notes']

recCore = ['Delete', 'Recommended', 'Keep', 'Keep + Modify', 'Keep + Modify + Store']
recPer = ['Delete', 'Archive', 'Archive + Modify', 'Archive + Modify + Store', 'Keep', 'Keep + Modify', 'Keep + Modify + Store']

date_string = time.strftime("%Y-%m-%d-%H%M")



workbook = xlsxwriter.Workbook('Test'+date_string+'.xlsx')
worksheet = workbook.add_worksheet()

headFormat = workbook.add_format({'font_color': 'white', 'bg_color': 'black'})
cellFormat = workbook.add_format({'border': 'style=1'})

row = 0
col = 0
row_count = 0

for r in recFields:
    worksheet.write(row, col, r, headFormat)
    col += 1
with open(csvfile) as infile:
    read = csv.reader(infile)


    for line in read:
        row_count += 1
        if row == 0:
            for l in line:
                worksheet.write(row, col, l, headFormat)
                col += 1
            row += 1
        else:
            col = 3
            for l in line:
                worksheet.write(row, col, l, cellFormat)
                col += 1
            row += 1

workbook.close()

csv文件输入如下:

Type,Release_Number,Release_Date,Number,Digital_Vaccine_Name,Category,NUM,Description,Standard Settings,Other Settings,Action
New,9630,"December 2, 2014  ",16999,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17044,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17065,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17067,Sample: sample-sample,Sample,2014-55,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17068,Sample: sample-sample,Sample,2014-56,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17071,Sample: sample-sample,Sample,2014-57,Example.,,default,keep+modify
New,9630,"December 2, 2014  ",17072,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17073,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17074,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17075,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17076,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17077,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17078,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17079,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
New,9630,"December 2, 2014  ",17080,Sample: sample-sample,Sample,,Example.,default,other-setting,keep+modify
Modified Filter,9630,"December 2, 2014  ",9543,Sample: sample-sample,,,,,,
Modified Filter,9630,"December 2, 2014  ",13299,Sample: sample-sample,,,,,,
Modified Filter,9630,"December 2, 2014  ",16797,Sample: sample-sample,,,,,default,
Modified Filter,9630,"December 2, 2014  ",16801,Sample: sample-sample,,,,,default,

我收到此错误:

Traceback (most recent call last):
  File "DV_ParseTest.py", line 68, in <module>
    workbook.close()
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/workbook.py", line 286, in close
    self._store_workbook()
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/workbook.py", line 509, in _store_workbook
    xml_files = packager._create_package()
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/packager.py", line 144, in _create_package
    self._write_styles_file()
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/packager.py", line 391, in _write_styles_file
    styles._assemble_xml_file()
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/styles.py", line 67, in _assemble_xml_file
    self._write_borders()
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/styles.py", line 391, in _write_borders
    self._write_border(xf_format)
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/styles.py", line 419, in _write_border
    xf_format.left_color)
  File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/styles.py", line 474, in _write_sub_border
    attributes.append(('style', border_styles[style]))
TypeError: tuple indices must be integers, not str

感谢任何帮助。

更新

我在这里更改了代码以编辑其他错误。我仍然有这个错误。

1 个答案:

答案 0 :(得分:3)

问题在于这一行:

cellFormat = workbook.add_format({'border': 'style=1'})

'border'的值应该只是一个整数,而不是一个字符串。你可能想要

cellFormat = workbook.add_format({'border': 1})