Python初学者在这里。我正在尝试在.conf文件中搜索特定标签,然后在子标签中插入新值:
<Dad bob>
<Child>
Child mike
Child ivan
Child george
</Child>
</Dad>
<Dad frank>
<Child>
Child carlos
</Child>
</Dad>
说我想为 Bob 插入一个新的 Child 。仅在找到</Child>
后,我的代码才应将Child newchild + "\n" + </Child>
替换为<Dad bob>
,然后停止。当我运行代码时,鲍勃和弗兰克都被添加了一个孩子。
不幸的是,这是我到目前为止所拥有的。
with fileinput.FileInput(CONF_FILE, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace('</Child>', '\t\t' + newchild + '\n\t</Child>'), end='')
我不知道怎么只用鲍勃代替。非常感谢您的帮助。 干杯!
答案 0 :(得分:0)
尝试一下:
from bs4 import BeautifulSoup
import re
with open("test.conf", "r") as f:
x = f.read()
soup = BeautifulSoup(x, 'lxml')
bobschild = soup.dad.child
for child in bobschild:
child.replace_with("Child newchild")
s = soup.prettify()
s = str(s)
s = s.replace("<html>","")
s = s.replace("<body>","")
s = s.replace("</body>","")
s = s.replace("</html>","")
t = ""
for line in s.split("\n"):
l = line[2:] + "\n"
if re.match(r'^\s*$', l):
continue
t += l
print(t)
with open("test.conf", "w") as f:
f.write(t)
这不是完美的,这是输出:
<dad bob="">
<child>
Child newchild
</child>
</dad>
<dad frank="">
<child>
Child carlos
</child>
</dad>
答案 1 :(得分:0)
感谢您的答复!
虽然不太漂亮,但我最终还是使用linux sed
命令在行tagOpen
和tagClose
之间进行替换:
MY_FILE = 'file.conf'
LOOKUP = '<Dad bob>'
LOOKUP_CLOSE = '</Dad>'
LOOKUP_CHILD_CLOSE = '</Child>'
CHILD_NAME = 'tony'
tagOpen = 0
tagClose = 0
stop = False
with open(MY_FILE) as myFile:
for num, line in enumerate(myFile, 1):
if LOOKUP in line:
tagOpen = num
stop = True
if LOOKUP_CLOSE in line and stop == True:
tagClose = num
break
if tagOpen != 0 or tagClose != 0:
os.system("sed -i \'%s,%ss/%s/\\tChild %s\\n\\t%s/\' %s" % (str(tagOpen), str(tagClose), LOOKUP_CHILD_CLOSE, CHILD_NAME, LOOKUP_CHILD_CLOSE, MY_FILE))