Python 3.4 lxml.etree:起始标签应为'<'找不到,第1行,第1列 [英] Python 3.4 lxml.etree: Start tag expected, '<' not found, line 1, column 1
问题描述
朋友
作为一名新手,鉴于论坛上提供的功能,我一直无法弄清楚.最终,我要做的只是获取一些简单的xml文件,并一次性将它们全部转换为CSV(尽管此代码一次只适用于一个).在我看来,没有正式的名称空间,但我不确定. 我有这段代码(我使用了一个标头'SubmittingSystemVendor',但我真的想将所有标头都写入CSV:
As a novice at best, I have not been able to figure this out given what is available in forums. Ultimately, all I want to do is take some simple xml files and convert them all to CSV in one go (though this code is just for one at a time). It looks to me like there are no official name spaces, but I'm not sure. I have this code (I used one header, 'SubmittingSystemVendor', but I really want to write all of them to CSV:
import csv
import lxml.etree
x = r'C:\Users\...\jh944.xml'
with open('output.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow('SubmittingSystemVendor')
root = lxml.etree.fromstring(x)
writer.writerow(row)
以下是XML文件的示例:
Here is a sample of the XML file:
<?xml version="1.0" encoding="utf-8"?>
<EOYGeneralCollectionGroup SchemaVersionMajor="2014-2015" SchemaVersionMinor="1" CollectionId="157" SubmittingSystemName="MISTAR" SubmittingSystemVendor="WayneRESA" SubmittingSystemVersion="2014" xsi:noNamespaceSchemaLocation="http://cepi.state.mi.us/msdsxml/EOYGeneralCollection2014-20151.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EOYGeneralCollection>
<SubmittingEntity>
<SubmittingEntityTypeCode>D</SubmittingEntityTypeCode>
<SubmittingEntityCode>82730</SubmittingEntityCode>
</SubmittingEntity>
提前谢谢!
推荐答案
您正在使用lxml.etree.fromstring
,但是给它一个文件路径作为参数.这意味着它将尝试将"C:\ Users ... \ jh944.xml"解释为要解析的XML数据.
You are using lxml.etree.fromstring
, but giving it a file path as the argument. This means it's trying to interpret "C:\Users...\jh944.xml" as the XML data to be parsed.
相反,您想打开包含该XML的文件 .您只需用lxml.etree.parse
替换对fromstring
的调用,它将接受文件名或打开的文件对象作为参数.
Instead, you want to open the file containing this XML. You can simply replace the call to fromstring
with lxml.etree.parse
, which will accept a filename or open file object as the argument.
这篇关于Python 3.4 lxml.etree:起始标签应为'<'找不到,第1行,第1列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!