SyntaxError:在前缀映射中找不到前缀“ a” [英] SyntaxError: prefix 'a' not found in prefix map
本文介绍了SyntaxError:在前缀映射中找不到前缀“ a”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个对 pptx
文档中的单词进行计数的函数。问题是我无法弄清楚如何只找到这种标签:
I'm trying to create a function which counts words in pptx
document. The problem is that I can't figure out how to find only this kind of tags:
< a:t>某些文本< / a:t>
当我尝试执行以下操作时: print xmlTree.findall('.// a:t' )
,它返回
When I try to: print xmlTree.findall('.//a:t')
, it returns
SyntaxError:在前缀映射中找不到前缀'a'
SyntaxError: prefix 'a' not found in prefix map
您知道该怎么做吗?
此功能:
def get_pptx_word_count(filename):
import xml.etree.ElementTree as ET
import zipfile
z = zipfile.ZipFile(filename)
i=0
wordcount = 0
while True:
i+=1
slidename = 'slide{}.xml'.format(i)
try:
slide = z.read("ppt/slides/{}".format(slidename))
except KeyError:
break
xmlTree = ET.fromstring(slide)
for elem in xmlTree.iter():
if elem.tag=='a:t':
#text = elem.getText
#num = len(text.split(' '))
#wordcount+=num
推荐答案
您需要告知 ElementTree
您的XML名称空间。
You need to tell ElementTree
about your XML namespaces.
参考文献:
- 官方文档(Python 2.7): 19.7.1.6。使用命名空间解析XML
- StackOverflow上的现有答案: 通过 ElementTree在XML中使用命名空间解析XML
- ElementTree作者的文章: 元素树:使用命名空间和限定名称
- Official Documentation (Python 2.7): 19.7.1.6. Parsing XML with Namespaces
- Existing answer on StackOverflow: Parsing XML with namespace in Python via 'ElementTree'
- Article by the author of ElementTree: ElementTree: Working with Namespaces and Qualified Names
这篇关于SyntaxError:在前缀映射中找不到前缀“ a”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文