在python中将Doc对象转换为字符串

发布于 2024-08-27 08:26:05 字数 446 浏览 3 评论 0原文

我正在使用 minidom 来解析 xml 文档。我用 yum 标签获取数据并将它们存储在列表中并计算单词的频率。但是,它不会将它们作为字符串存储或读取在列表中。还有其他方法吗?现在这就是我所拥有的:

yumNodes = [node for node in doc.getElementsByTagName("yum")]

for node in yumNodes:
    yumlist.append(t.data for t in node.childNodes if t.nodeType == t.TEXT_NODE)

for ob in yumlist:
    for o in ob:
        if word not in freqDict:
            freqDict[word] = 1
        else:
            freqDict[word] += 1

I'm using minidom to parse through an xml document. I took the data with yum tags and stored them in a list and calculated the frequency of the words. However, its not storing or reading them as strings in the list. Is there another way to do it? Right now this is what I have:

yumNodes = [node for node in doc.getElementsByTagName("yum")]

for node in yumNodes:
    yumlist.append(t.data for t in node.childNodes if t.nodeType == t.TEXT_NODE)

for ob in yumlist:
    for o in ob:
        if word not in freqDict:
            freqDict[word] = 1
        else:
            freqDict[word] += 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半﹌身腐败 2024-09-03 08:26:06

与您的问题没有直接关系,但作为可以改进您的代码的评论...该模式

freqDict = {}
...
if word not in freqDict:
    freqDict[word] = 1
else:
    freqDict[word] += 1

通常被替换为

import collections
freqDict = collections.defaultdict(int)
...
freqDict[word] += 1

或 2.5 之前的版本

freqDict = {}
...
freqDict.setdefault(word, 0) += 1

Not directly related to your question, but as a remark that could improve your code...the pattern

freqDict = {}
...
if word not in freqDict:
    freqDict[word] = 1
else:
    freqDict[word] += 1

is usually replaced with

import collections
freqDict = collections.defaultdict(int)
...
freqDict[word] += 1

or pre-2.5

freqDict = {}
...
freqDict.setdefault(word, 0) += 1
德意的啸 2024-09-03 08:26:06

替换

yumlist.append(t.data for t in node.childNodes if t.nodeType == t.TEXT_NODE)

为以下内容:

yumlist.append(t.nodeValue for t in node.childNodes if t.nodeType == 3)

Replace

yumlist.append(t.data for t in node.childNodes if t.nodeType == t.TEXT_NODE)

with the following:

yumlist.append(t.nodeValue for t in node.childNodes if t.nodeType == 3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文