以下是小编帮大家整理的python比较2个xml内容的方法,本文共3篇,仅供参考,大家一起来看看吧。

篇1:python比较2个xml内容的方法

作者:像风一样的自由 字体:[增加 减小] 类型:

这篇文章主要介绍了python比较2个xml内容的方法,涉及Python操作XML文件的相关技巧,需要的朋友可以参考下

本文实例讲述了python比较2个xml内容的方法,分享给大家供大家参考。具体分析如下:

from xml.etree import ElementTree K=True main_pid = 10000 loop_depth = 0 def compare_xml(left, right, key_info=‘.‘): global loop_depth loop_depth += 1 if loop_depth == 1: print if left.tag != right.tag: print_diff(main_pid, key_info, ‘difftag‘, left.tag, right.tag) return if left.text != right.text: print_diff(main_pid, key_info, ‘difftext‘, left.text, right.text) return leftitems = dict(left.items) rightitems = dict(right.items()) for k,v in leftitems.items(): if k not in rightitems: s = ‘%s/%s‘ % (key_info, left.tag) print_diff(main_pid, s, ‘lostattr‘, k, “”) for k,v in rightitems.items(): if k not in leftitems: s = ‘%s/%s‘ % (key_info, right.tag) print_diff(main_pid, s, ‘extraattr‘, “”, k) leftnodes = left.getchildren() rightnodes = right.getchildren() leftlen = len(leftnodes) rightlen = len(rightnodes) if leftlen != rightlen: s = ‘%s/%s‘ % (key_info, right.tag) print_diff(main_pid, s, ‘difflen‘, leftlen, rightlen) return l = leftlen%-40s [ %s != %s ]‘%(msg.upper(), main_pid, key_info.strip(‘./‘), base_type, test_type) print info.encode(‘gbk‘) K = False

调用:

if __name__ == ‘__main__‘: s1 = ‘‘‘‘‘<?xml version=“1.0” encoding=“UTF-8”?>\ \\ linux\ 30\\\ windows\ 20\\‘‘‘ s2 = ‘‘‘‘‘<?xml version=“1.0” encoding=“UTF-8”?>\ \\ windows\ 20\\\ linux\ 30\\‘‘‘ lroot = ElementTree.fromstring(s1) rroot = ElementTree.fromstring(s2) compare_xml(lroot, rroot)

希望本文所述对大家的Python程序设计有所帮助,

篇2:python提取内容关键词的方法

作者:上大王 字体:[增加 减小] 类型:转载

这篇文章主要介绍了python提取内容关键词的方法,适用于英文关键词的提取,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了python提取内容关键词的方法,分享给大家供大家参考。具体分析如下:

一个非常高效的提取内容关键词的python代码,这段代码只能用于英文文章内容,中文因为要分词,这段代码就无能为力了,不过要加上分词功能,效果和英文是一样的。

代码如下:

# coding=UTF-8

import nltk

from nltk.corpus import brown

# This is a fast and simple noun phrase extractor (based on NLTK)

# Feel free to use it, just keep a link back to this post

# thetokenizer//05/09/efficient-way-to-extract-the-main-topics-of-a-sentence/

# Create by Shlomi Babluki

# May, 2013

# This is our fast Part of Speech tagger

#############################################################################

brown_train = brown.tagged_sents(categories=‘news‘)

regexp_tagger = nltk.RegexpTagger(

[(r‘^-?[0-9]+(.[0-9]+)?$‘, ‘CD‘),

(r‘(-|:|;)$‘, ‘:‘),

(r‘\‘*$‘, ‘MD‘),

(r‘(The|the|A|a|An|an)$‘, ‘AT‘),

(r‘.*able$‘, ‘JJ‘),

(r‘^[A-Z].*$‘, ‘NNP‘),

(r‘.*ness$‘, ‘NN‘),

(r‘.*ly$‘, ‘RB‘),

(r‘.*s$‘, ‘NNS‘),

(r‘.*ing$‘, ‘VBG‘),

(r‘.*ed$‘, ‘VBD‘),

(r‘.*‘, ‘NN‘)

])

unigram_tagger = nltk.UnigramTagger(brown_train, backoff=regexp_tagger)

bigram_tagger = nltk.BigramTagger(brown_train, backoff=unigram_tagger)

#############################################################################

# This is our semi-CFG; Extend it according to your own needs

#############################################################################

cfg = {}

cfg[“NNP+NNP”] = “NNP”

cfg[“NN+NN”] = “NNI”

cfg[“NNI+NN”] = “NNI”

cfg[“JJ+JJ”] = “JJ”

cfg[“JJ+NN”] = “NNI”

#############################################################################

class NPExtractor(object):

def __init__(self, sentence):

self.sentence = sentence

# Split the sentence into singlw words/tokens

def tokenize_sentence(self, sentence):

tokens = nltk.word_tokenize(sentence)

return tokens

# Normalize brown corpus‘ tags (“NN”, “NN-PL”, “NNS” >“NN”)

def normalize_tags(self, tagged):

n_tagged = []

for t in tagged:

if t[1] == “NP-TL” or t[1] == “NP”:

n_tagged.append((t[0], “NNP”))

continue

if t[1].endswith(“-TL”):

n_tagged.append((t[0], t[1][:-3]))

continue

if t[1].endswith(“S”):

n_tagged.append((t[0], t[1][:-1]))

continue

n_tagged.append((t[0], t[1]))

return n_tagged

# Extract the main topics from the sentence

def extract(self):

tokens = self.tokenize_sentence(self.sentence)

tags = self.normalize_tags(bigram_tagger.tag(tokens))

merge = True

while merge:

merge = False

for x in range(0, len(tags) - 1):

t1 = tags[x]

t2 = tags[x + 1]

key = “%s+%s” % (t1[1], t2[1])

value = cfg.get(key, ‘‘)

if value:

merge = True

tags.pop(x)

tags.pop(x)

match = “%s %s” % (t1[0], t2[0])

pos = value

tags.insert(x, (match, pos))

break

matches = []

for t in tags:

if t[1] == “NNP” or t[1] == “NNI”:

#if t[1] == “NNP” or t[1] == “NNI” or t[1] == “NN”:

matches.append(t[0])

return matches

# Main method, just run “python np_extractor.py”

def main():

sentence = “Swayy is a beautiful new dashboard for discovering and curating online content.”

np_extractor = NPExtractor(sentence)

result = np_extractor.extract()

print “This sentence is about: %s” % “, ”.join(result)

if __name__ == ‘__main__‘:

main()

希望本文所述对大家的Python程序设计有所帮助,

篇3:python 七种邮件内容发送方法实例

最近更 新

python访问sqlserver示例

Python open读写文件实现脚本

python 控制语句

python计算程序开始到程序结束的运行时间

Python去掉字符串中空格的方法

python函数缺省值与引用学习笔记分享

python原始套接字编程示例分享

Python里隐藏的“禅”

删除目录下相同文件的python代码(逐级优化

python抓取京东价格分析京东商品价格走势

热 点 排 行

Python入门教程 超详细1小时学会

python 中文乱码问题深入分析

比较详细Python正则表达式操作指

Python字符串的encode与decode研

Python open读写文件实现脚本

Python enumerate遍历数组示例应

Python 深入理解yield

Python+Django在windows下的开发

python 文件和路径操作函数小结

python 字符串split的用法分享

更多推荐

python比较2个xml内容的方法