我用python写了段删除xml节点的程序,结果输出到另一个xml文件中,怎么保证输出后的各节点的格式不变啊?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我用python写了段删除xml节点的程序,结果输出到另一个xml文件中,怎么保证输出后的各节点的格式不变啊?相关的知识,希望对你有一定的参考价值。

参考技术A 例如你读取的xml文件存放到a中,然后你对a中某段xml进行删除,删除你把a保存到另一个xml中就可以了追问

现在我是删除一个节点,找到那个节点直接删除,然后将更改的xml写到一个新的xml文件中,报了个错:
TypeError: write() got an unexpected keyword argument 'xml_declaration'
下面是我实现写入xml文件的函数体:
tree.write(out_path, encoding="utf-8",xml_declaration=True)
这是哪儿的错啊

追答

你多粘出点代码,我的qq是1147162341,你可以加我,我们在交流

来自:求助得到的回答
参考技术A 还是用现成的beautifulsoap工具吧

我用python写了如下程序,可是后来最后出了问题。有谁帮我解决一下??

import math
from operator import itemgetter

import nltk.data
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import os
import collections
from string import punctuation
aynur=open('f:\\guli3.txt')
ahmat=aynur.read()
import nltk.data
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import os
tokenizer=nltk.data.load('tokenizers/punkt/english.pickle')
words=word_tokenize(ahmat)
uygur_stops=set(stopwords.words('uygur'))
words1=words
words2=[word for word in words1 if word not in uygur_stops]

def freq(word, document):
return document.split(None).count(word)

def wordCount(document):
return len(document.split(None))

def numDocsContaining(word,documentList):
count = 0
for document in documentList:
if freq(word,document) > 0:
count += 1
return count

def tf(word, document):
return (freq(word,document) / float(wordCount(document)))

def idf(word, documentList):
return math.log(len(documentList) / numDocsContaining(word,documentList))

def tfidf(word, document, documentList):
return (tf(word,document) * idf(word,documentList))

if __name__ == '__main__':
documentList = []
documentList.append("f:\\guli3.txt")
documentList.append("""DOCUMENT #2 TEXT""")
documentList.append("""DOCUMENT #3 TEXT""")
words6 =
words6=words2
documentNumber = 0
for word5 in documentList[documentNumber].split(None):
words6[word5] = tfidf(word5,documentList[documentNumber],documentList)
for item in sorted(words.items(), key=itemgetter(1), reverse=True):
print "%f <= %s" % (item[1], item[0])

出现问题如下:
>>>
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\桌面\1.py", line 55, in <module>
words6[word5] = tfidf(word5,documentList[documentNumber],documentList)
TypeError: list indices must be integers
>>>

words2是列表,所以words6也是列表,不是字典 参考技术A 你好:
error作为提示已经很清楚了,list中必须是整型变量
希望对你有帮助

以上是关于我用python写了段删除xml节点的程序,结果输出到另一个xml文件中,怎么保证输出后的各节点的格式不变啊?的主要内容,如果未能解决你的问题,请参考以下文章

最近开始努力学python 写了一个python小代码:判断一个登陆程序,如果账号密码输错3次,锁定账号无法再登陆

如何使用递归的方法获得指定文件夹下某个指定文件的完整路径?我自己写了段代码,但是有些问题,请指教

python xml.dom.minidom 操作xml, 删除节点的问题

我用python写了如下程序,可是后来最后出了问题。有谁帮我解决一下??

分治法解决凸包问题到底咋回事?为了弄懂,我用python写了个可视化窗体程序

python -mSimpleHTTPServer 开启一个简单的http服务器,可传输文件