前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >raise ValueError("Cannot convert {0!r} to Excel".format(value))

raise ValueError("Cannot convert {0!r} to Excel".format(value))

作者头像
hankleo
发布2020-09-17 09:59:39
3K0
发布2020-09-17 09:59:39
举报
文章被收录于专栏:Hank’s BlogHank’s Blog

I have hundreds of XML files that I need to extract two values from and ouput in an Excel or CSV file. This is the code I currently have:

#grabs idRoot and typeId root values from XML files
import glob
from openpyxl import Workbook
from xml.dom import minidom
import os


wb = Workbook()
ws = wb.active
def typeIdRoot (filename):

    f = open(filename, encoding = "utf8")
    for xml in f:

        xmldoc = minidom.parse(f)

        qmd = xmldoc.getElementsByTagName("MainTag")[0]

        typeIdElement = qmd.getElementsByTagName("typeId")[0]

        root = typeIdElement.attributes["root"]

        global rootValue
        rootValue = root.value
    print ('rootValue =' ,rootValue,)
    ws.append([rootValue])
    wb.save("some.xlsx")   



wb = Workbook()
ws = wb.active
def idRoot (filename):

    f = open(filename, encoding = "utf8")
    for xml in f:

        xmldoc = minidom.parse(f)

        tcd = xmldoc.getElementsByTagName("MainTag")[0]

        activitiesElement = tcd.getElementsByTagName("id")[0]

        sport = activitiesElement.attributes["root"]

        sportName = sport.value

        print ('idRoot =' ,sportName,)

        ws.append([idRoot])

        wb.save("some.xlsx")    


for file in glob.glob("*.xml"):
    typeIdRoot (file)

for file in glob.glob("*.xml"):
    idRoot (file)

The first value follows a 1.11.111.1.111111.1.3 format. The second mixes letters and numbers. I believe this is the reason for the error:

Traceback (most recent call last):
  File "C:\Python34\Scripts\xml\good.py", line 64, in <module>
    idRoot (file)
  File "C:\Python34\Scripts\xml\good.py", line 54, in idRoot
    ws.append([idRoot])
  File "C:\Python34\lib\site-packages\openpyxl\worksheet\worksheet.py", line 754, in append
    cell = self._new_cell(col, row_idx, content)
  File "C:\Python34\lib\site-packages\openpyxl\worksheet\worksheet.py", line 376, in _new_cell
    cell = Cell(self, column, row, value)
  File "C:\Python34\lib\site-packages\openpyxl\cell\cell.py", line 131, in __init__
    self.value = value
  File "C:\Python34\lib\site-packages\openpyxl\cell\cell.py", line 313, in value
    self._bind_value(value)
  File "C:\Python34\lib\site-packages\openpyxl\cell\cell.py", line 217, in _bind_value
    raise ValueError("Cannot convert {0} to Excel".format(value))
ValueError: Cannot convert <function idRoot at 0x037D24F8> to Excel

I would like the result to add both values on the same row. So then I would have a new row for each file in the directory. I need to add the second value to the second row.

as such:

  Value 1                           Value 2
1.11.111.1.111111.1.3           10101011-0d10-0101-010d-0dc1010e0101

answer 1 >>解决方法

idRoot is the name of your FUNCTION. So when you write

ws.append([idRoot])

you probably mean:

ws.append([sportName])

Of course, you can write something like:

ws.append([rootValue, sportName])

providing both variables are defined with reasonable values. One last thing, you should save your file only once.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档