我正在尝试使用 cElementTree 保存编码为 UTF-16 的 XML 文件。这是同一个项目,但与以下中的 DOCTYPE 问题不同:How to create <!DOCTYPE> with Python's cElementTree
我了解到,如果我没有在字符串中声明编码,cElementTree 将添加它。所以,代码是这样的:
import xml.etree.cElementTree as ElementTree
from StringIO import StringIO
s = '<?xml version=\"1.0\" ?><!DOCTYPE tmx SYSTEM \"tmx14a.dtd\" ><tmx version=\"1.4a\" />'
tree = ElementTree.parse(StringIO(s)).getroot()
header = ElementTree.SubElement(tree,'header',{'adminlang': 'EN',})
body = ElementTree.SubElement(tree,'body')
ElementTree.ElementTree(tree).write('myfile.tmx','UTF-16')
Run Code Online (Sandbox Code Playgroud)
当我用 UTF-8 编写文件时,一切都很好。但是,当我更改为 UTF-16 时,文本编码已损坏。它还缺少所需的字节顺序标记。当我尝试将 BOM 添加到字符串的开头时,
s = '\xFF\xFE<?xml version=\"1.0\"......
Run Code Online (Sandbox Code Playgroud)
ElementTree 报告错误“格式不正确(无效标记)第 1 行第 1 列”。
所有缓冲区都是 unicode 数据。如何保存为 UTF-16 XML 文件?
我有一个如下所示的 XML(简化版):
<file id="file-10">
<clip>1</clip>
<timecode>1:00:00:00</timecode>
</file>
<file id="file-11">
<clip>2</clip>
<timecode>2:00:00:00</timecode>
</file>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 ElementTree 搜索具有特定 id 属性的文件元素。这有效:
correctfile = root.find('file[@id="file-10"]')
Run Code Online (Sandbox Code Playgroud)
这不会:
fileid = 'file-10'
correctfile = root.find('file[@id=fileid]')
Run Code Online (Sandbox Code Playgroud)
我得到:
语法错误:谓词无效
这是一个限制吗ElementTree?我应该使用其他东西吗?
是否可以通过管道将变量传递给@name属性?
import xml.etree.ElementTree as ET\ntree = ET.parse(\'C:/test.xml\')\nroot = tree.getroot()\n\nsomelist = [x.text for x in root.findall(".//actionList[@name=\'VARIABLEHEREinsteadoftext\']//value")]\nRun Code Online (Sandbox Code Playgroud)\n\n我需要仅从按名称过滤的特定操作列表中获取所有值,并忽略所有其他操作列表。它可以很好地与
\n\n[@name="ACTIONLISTNAME"]\nRun Code Online (Sandbox Code Playgroud)\n\n但我\xc2\xb4d喜欢这样的东西:
\n\nX = ACTIONLISTNAME\n[@name=X]\nRun Code Online (Sandbox Code Playgroud)\n\n提前致谢!
\n我需要为 Azure TTS 创建此标头:
<speak version="1.0"
xmlns="https://www.w3.org/2001/10/synthesis"
xmlns:mstts="https://www.w3.org/2001/mstts"
xml:lang="en-US">
Run Code Online (Sandbox Code Playgroud)
这是用于创建 xml:lang 键的代码:
xml_body = ElementTree.Element('speak', version='1.0')
xml_body.set('{http://www.w3.org/XML/1998/namespace}lang', 'en-us')
Run Code Online (Sandbox Code Playgroud)
我试图创建 xmlns:mstts 没有成功。这不起作用:
xml_body.set('{https://www.w3.org/2001/10/synthesis}mstts', 'https://www.w3.org/2001/mstts' )
Run Code Online (Sandbox Code Playgroud)
因为这会产生以下输出:
<speak version="1.0"
xmlns="https://www.w3.org/2001/10/synthesis"
xmlns:mstts="https://www.w3.org/2001/mstts"
xml:lang="en-US">
Run Code Online (Sandbox Code Playgroud)
请注意元素属性中的xmlns:ns0和ns0:mstts问题<speak>。
有任何想法吗?
作为 Python 的初学者,我正在尝试使用本教程说明(https://www.youtube.com/watch?v=kq2Gjv_pPe8&list=PLiIy2ThQvgewp67FDKV2H1h-154bJK9RS&index=2&t=477s)将我的 XML 文件转换为 CSV 。最后,我需要 tfrecord 格式的图像和注释,以便在我的自定义 EfficientDet 模型中使用它们。我遵循了这两个帖子的解决方案(IndexError: child index out of range以及为什么我不断让 child out of range 错误?)并尝试了一堆这句话中不同节点号(1-9)的“int(member[3][0].text)”却不断收到“IndexError: child index out of range”错误!
我正在尝试使用以下格式转换我的 XML 文件:
<annotation>
<folder>images</folder>
<filename>Czech_000010.jpg</filename>
<size>
<depth>3</depth>
<width>600</width>
<height>600</height>
</size>
<object>
<name>D40</name>
<bndbox>
<xmin>213</xmin>
<ymin>409</ymin>
<xmax>274</xmax>
<ymax>441</ymax>
</bndbox>
</object>
<object>
<name>D10</name>
<bndbox>
<xmin>228</xmin>
<ymin>473</ymin>
<xmax>327</xmax>
<ymax>495</ymax>
</bndbox>
</object>
</annotation>
Run Code Online (Sandbox Code Playgroud)
使用以下脚本转换为 CSV:
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
def xml_to_csv(path):
xml_list = …Run Code Online (Sandbox Code Playgroud) 我想创建一个 XML 文件。我试过了,但格式看起来很糟糕,它只显示一行。
就这个:
import xml.etree.cElementTree as ET
root = ET.Element("data")
doc = ET.SubElement(root, "status", date="20210123")
ET.SubElement(doc, "name", name="john").text = "some value1"
ET.SubElement(doc, "class", name="abc").text = "some vlaue2"
tree = ET.ElementTree(root)
tree.write("FILE.xml")
Run Code Online (Sandbox Code Playgroud)
输出是:
<data><status date="20210123"><name name="john">some value1</name><class name="abc">some vlaue2</class></status></data>
Run Code Online (Sandbox Code Playgroud)
但我的期望输出是:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<status>
<name name="john">some value1</name>
<class name="abc">some vlaue2</class>
</status>
</data>
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我一个想法,请。我真的很感激。谢谢
我正在尝试使用 Python 的 xml 模块解析 XML 文件中的信息。问题是,当我指定文件列表并开始解析策略时,在(据称)成功解析第一个文件后,我收到以下错误:
Parsing 20586908.xml ..
Parsing 20586934.xml ..
Traceback (most recent call last):
File "<ipython-input-72-0efdae22e237>", line 11, in parse
xmlTree = ET.parse(xmlFilePath, parser = self.parser)
File "C:\Users\StefanCepa995\miniconda3\envs\dl4cv\lib\xml\etree\ElementTree.py", line 1202, in parse
tree.parse(source, parser)
File "C:\Users\StefanCepa995\miniconda3\envs\dl4cv\lib\xml\etree\ElementTree.py", line 601, in parse
parser.feed(data)
xml.etree.ElementTree.ParseError: parsing finished: line 1755, column 0
Run Code Online (Sandbox Code Playgroud)
这是我用来解析 XML 文件的代码:
class INBreastXMLParser:
def __init__(self, xmlRootDir):
self.parser = ET.XMLParser(encoding="utf-8")
self.xmlAnnotations = [os.path.join(root, f)
for root, dirs, files in os.walk(xmlRootDir)
for f in files if f.endswith('.xml')]
def …Run Code Online (Sandbox Code Playgroud) 我有一个xml文件.
<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Salary</GroupName>
<EditId>undefined</EditId>
Run Code Online (Sandbox Code Playgroud)
<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Salary</GroupName>
<EditId>undefined</EditId>
Run Code Online (Sandbox Code Playgroud)
<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Trfr fm Savings</GroupName>
<EditId>undefined</EditId>
Run Code Online (Sandbox Code Playgroud)
<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Dividend</GroupName>
<EditId>undefined</EditId>
Run Code Online (Sandbox Code Playgroud)
<Item>Item value</Item>
<Itemdate>24/07/2010</Itemdate>
<Total>1</Total>
<Itemcategory>Income</Itemcategory>
<GroupName>Dividend</GroupName>
<EditId>undefined</EditId>
Run Code Online (Sandbox Code Playgroud)
现在我想得到所有项目,itemdate等分别使用elementtree.任何人都可以帮助我吗?
RGDS,
Nimmy
我已经搜索了一段时间。有没有一种方法可以根据标签的文本值直接检索子级?
例如:
<a>
<b>
<c>h</c>
</b>
<b>
<c>j</c>
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
并说我想检索其“ c”文本值为== j的孩子。除了必须获取所有“ b”子代并遍历它们并检查c值之外,还有其他方法吗?
我在html文档中有以下文本:
<a href="#">?'?? ????????? ??????????</a>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下表达式来提取文本:
row.xpath("string(./td[@class='col2 td-tags']/h3/a/text())")
Run Code Online (Sandbox Code Playgroud)
此表达式适用于简单的英语,但对于上面的字符串,它会抛出此错误:
'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte
Run Code Online (Sandbox Code Playgroud) elementtree ×10
python ×10
xml ×6
xml-parsing ×3
xpath ×3
csv ×1
findall ×1
pretty-print ×1
tensorflow ×1