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.

raise ValueError("Cannot convert {0!r} to Excel".format(value))的更多相关文章

  1. 解决: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19

    错误信息:C:\Python27\lib\site-packages\sklearn\utils\validation.py:395: DeprecationWarning: Passing 1d a ...

  2. XLConnect:一个用R处理Excel文件的高效平台

    code{white-space: pre;} pre:not([class]) { background-color: white; }if (window.hljs && docu ...

  3. Cannot convert 0 of type class java.lang.Integer to class java.lang.Boolean

    org.apache.catalina.core.ApplicationDispatcher invoke SEVERE: Servlet.service() for servlet jsp thre ...

  4. raise PDFEncryptionError('Unknown algorithm: param=%r' % param) pdfminer.pdfdocument.PDFEncryptionError: Unknown algorithm

    使用pdfminer遇到的pdf文件加密问题: raise PDFEncryptionError('Unknown algorithm: param=%r' % param) pdfminer.pdf ...

  5. R读取excel文件

    2017.09.05 我一个下午的成果啊啊啊啊,看看失败 不禁感叹一声,失败的路真是多啊!!!! 一.安装xlsx包 下面具体讲一讲怎么弄的(太笨了,所以学得慢,需要一步一步的来) 用R读取excel ...

  6. Pycharm Fiddler Requests https in _create raise ValueError("check_hostname requires server_hostname

    打开Fiddler, 开启抓取https,  在PyCharm中使用requests 发送https请求, 遇到  in _create raise ValueError("check_ho ...

  7. .NET使用NPOI2.0导入导出Excel

    NPOI开源地址:http://npoi.codeplex.com/ NPOI教程: http://tonyqus.sinaapp.com/ 具体的不在这里写了,感兴趣的可以去官网. 先来说导出的例子 ...

  8. thinkphp5.0数据导出excel表格

    第一步.创建Model类文件(名称自定) 第二步.在类中写入以下代码 <?php namespace Admin\admin\model; use think\Model; class Mark ...

  9. Oracle EBS Report 输出字符字段前部"0"被Excel自动去掉问题

    Oracle  EBS 提供多种报表的开发和输出形式,由于MS Excel在处理数据方面的优势明显,报表输出用Excel打开是很常见的开发项. 但是正是由于Excel的"过于智能而不智能&q ...

随机推荐

  1. [nodejs] nodejs开发个人博客(一)准备工作

    前言 nodejs是运行在服务端的js,基于google的v8引擎.个人博客系统包含对数据库的增删查改,功能齐备,并且业务逻辑比较简单,是很多后台程序员为了检测学习成果,最先拿来练手的小网站程序.我也 ...

  2. Android破解学习之路(十六)—— dll破解的IL指令

    IL指令介绍 IL是.NET框架中中间语言(Intermediate Language)的缩写. 使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不 ...

  3. D. GukiZ and Binary Operations(矩阵+二进制)

    D. GukiZ and Binary Operations   We all know that GukiZ often plays with arrays. Now he is thinking ...

  4. ES6核心特性

    摘要:聊JS离不开ES6啊! 原文:ES6核心特性 作者:ljianshu 前言 ES6 虽提供了许多新特性,但我们实际工作中用到频率较高并不多,根据二八法则,我们应该用百分之八十的精力和时间,好好专 ...

  5. javascript模块化编程-立即执行函数(IIFE)

    IIFE 全拼Imdiately Invoked Function Expression,立即执行的函数表达式. 语法 var module1 = (function(){ var _count = ...

  6. thinkphp 使用paginate分页搜索带参数

    最近做项目发现使用paginate分页,搜索的时候点下一页搜索条件就变没了,所以在网上找了找一些方法,有的说是使用Page类,但是用习惯了paginate,再用Page不习惯,找到了一个方法,可以使用 ...

  7. 关系数据库标准语言SQL——概述

      SQL是一种介于关系代数与关系演算之间的结构化查询语言,其功能并不仅仅是查询.SQL是一个通用的.功能极强的关系数据库语言.SQL(Structured Query Language)结构化查询语 ...

  8. 数据分析 - 美国金融科技公司Prosper的风险评分分析

    数据分析 - 美国金融科技公司Prosper的风险评分分析 今年Reinhard Hsu觉得最有意思的事情,是参加了拍拍贷第二届魔镜杯互联网金融数据应用大赛.通过"富爸爸队",认识 ...

  9. PVS桌面主镜像配置后,实际用户登录,配置未生效

    1.打开系统属性——高级——用户配置文件下的[设置] 2.打开用户配置文件,可以看到[复制]项灰化 3.使用windwows enable 工具启动上述灰化项,运行附件的exe文件后,任务栏出现下图标 ...

  10. mysql练习----The JOIN operation

    game id mdate stadium team1 team2 1001 8 June 2012 National Stadium, Warsaw POL GRE 1002 8 June 2012 ...