简体   繁体   中英

Can't write to Excel file from Python

I'm trying to write to an Excel file using Python and the xlrd module , with the following code:

Table = [ 'baseMax - 1cm', 'baseMin- 1cm',
          'baseMax - 5cm', 'baseMin - 5cm',
          'baseMax - 15cm' 'baseMin - 15cm'
        ]
new_base = { 'baseMax - 1cm': [], 'baseMax - 5cm': [],
             'baseMax - 15cm': [], 'baseMin- 1cm': [],
             'baseMin - 5cm': [], 'baseMin - 15cm': []
           }
HeadName = 'Base Calculation'
for i in Table:
    base_stats.write(ROW, 0, HeadName + ' ' + i + ' - ')
    base_stats_stats.write(ROW, 1, new_base[i])
    ROW = ROW+1
    ROW = ROW+1

When I run this code I get an error at new_base[i] . I'm not sure I understand exactly what this error is about. It appears to have to do with the parameters I'm passing, but I don't really understand it.

raise Exception("Unexpected data type %r" % type(label))
  Exception: Unexpected data type <type 'list'>

I assume that your variable names and formatting are correct in your actual code.

When you're using the write method you're not giving it the correct parameters.

The following method prototype is found in the xlwt module documentation : write(r, c, label="", style=Style.default_style)

The label parameter apparently requires a string. When you reference new_base[i] you are getting back an empty list:

'baseXxx - Ncm':[]

Thus, when the write method looks for a string and instead gets [] , it throws an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM