简体   繁体   English

无法从Python写入Excel文件

[英]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: 我正在尝试使用Python和xlrd模块使用以下代码写入Excel文件:

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] . 当我运行此代码时,在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. 当您使用write方法时,您没有为其提供正确的参数。

The following method prototype is found in the xlwt module documentation : write(r, c, label="", style=Style.default_style) xlwt模块文档中找到以下方法原型:write(r,c,label =“”,style = Style.default_style)

The label parameter apparently requires a string. label参数显然需要一个字符串。 When you reference new_base[i] you are getting back an empty list: 当您引用new_base[i]您将返回一个空列表:

'baseXxx - Ncm':[]

Thus, when the write method looks for a string and instead gets [] , it throws an exception. 因此,当write方法查找字符串并获取[] ,它将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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