我有一个大的xml文件,我只想将import re
pat = re.compile("current from")
current = {}
with open(fileName) as f:
for line in f:
if pat.search(line):
key1 = (line.split()[2])
elif line != "\n" :
current[key1][line.split()[0]].append(line.split()[2])
for key1 in current:
for key2 in current[key1]:
avg = ((current[key1][key2] + current[key2][key1])/2)
print("current " + key1 + "-" + key2 + " is " + str(avg))
替换为weight="7.0"
,但只能在包含weight="11.0"
的行中替换,如下所示。
clockRateScaler
我尝试了 <operator id="clockRateScaler." spec="ScaleOperator" parameter="@clockRate.c" scaleFactor="0.5" weight="7.0"/>
sed
但它不起作用。
我怎么能在awk或sed下做这个?
答案 0 :(得分:1)
使用features = {
'image/encoded': bytes_feature(image_data),
'image/format': bytes_feature(image_format),
'image/class/fine_label': int64_feature(fine_class_id),
'image/class/coarse_label': int64_feature(coarse_class_id),
'image/height': int64_feature(height),
'image/width': int64_feature(width),
}
sed
使用sed '/clockRateScaler/s/weight="7.0"/weight="11.0"/g' file.xml
awk
或使用awk '/clockRateScaler/{gsub("weight=\"7.0\"", "weight=\"11.0\"")}1' file.xml
(更好)
xmlstarlet
你明白了,
xmlstarlet ed -u '/operator[@id="clockRateScaler."]/@weight' -v 11.0 file.xml
答案 1 :(得分:1)
您正在尝试使用相同的字符串替换weight="7.0"
,而您错过了一些/
。
你应该尝试命令:
sed '/clockRateScaler/s/weight="7.0"/weight="11.0"/' file.xml