Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要帮助解析XML文件中的标记并用新值替换它们吗_Python_Python 2.7_Xml Parsing - Fatal编程技术网

Python 需要帮助解析XML文件中的标记并用新值替换它们吗

Python 需要帮助解析XML文件中的标记并用新值替换它们吗,python,python-2.7,xml-parsing,Python,Python 2.7,Xml Parsing,我正试图修改几周前的电话号码脚本来帮助一个朋友。这是我作为起点使用的脚本 # import regular expressions import re # import argv from sys import argv #arguments to provide at command line script, filename = argv #load the file data = open(filename) #read the file read_file = data.rea

我正试图修改几周前的电话号码脚本来帮助一个朋友。这是我作为起点使用的脚本

# import regular expressions 
import re
# import argv 
from sys import argv

#arguments to provide at command line 
script, filename = argv

#load the file
data = open(filename)
#read the file
read_file = data.read()

# create a regular expression to filter out phone numbers 
phone_finder = re.compile(r"\(\d{3}\)\s*\d{3}-\d{4}")

# r to tell its a raw string
# \( to match "("
# \d{3} to match 3 digits
# \) to match ")"
# \s* account for no spaces
# \d{3} to match 3 digits
# - to match an "-"
# \d{4} to match 4 digits

# print the results
print phone_finder.findall(read_file)
他想要一种搜索XML文件并查找“


要从
元素中删除所有内容:

import xml.etree.cElementTree as etree

etree.register_namespace('excerpt', 'your namespace') # to preserve prefix

# read xml
doc = etree.parse(filename)

# clear elements
for element in doc.iter(tag='{your namespace}encoded'): 
    element.clear()

# write xml
doc.write(filename + '.cleared')
您应该将
“您的名称空间”
替换为实际的名称空间
摘录
前缀引用

<excerpt:encoded><![CDATA[]]></excerpt:encoded>
import xml.etree.cElementTree as etree

etree.register_namespace('excerpt', 'your namespace') # to preserve prefix

# read xml
doc = etree.parse(filename)

# clear elements
for element in doc.iter(tag='{your namespace}encoded'): 
    element.clear()

# write xml
doc.write(filename + '.cleared')