我在更改文本文件中特定行中某些单词的位置时遇到了一些问题,如下所示:
product name="anything" s_c="7" act_ferq="UREWUERWERU" sys_name="mhr22:21140017-21140076"
common name="xyz" value="1234"
common
product name="anything" s_c="7" control_type="pos" act_ferq="UREWUERWERU"
sys_name="mhr22:21140017-21140076"
common name="xyz" value="1234"
common
product name="anything" s_c="7" act_ferq="UREWUERWERU" sys_name="mhr22:21140016-21140079"
common name="xyz" value="1234"
common
product name="anything" s_c="7" control_type="neg" act_ferq="UREWUERWERU"
sys_name="mhr22:21140017-21140076"
common name="xyz" value="1234"
common
我希望它看起来像:
product name="anything" sys_name="mhr22:21140017-21140076 act_ferq="UREWUERWERU" s_c="7"
common name="xyz" value="1234"
common
product name="anything" sys_name="mhr22:21140017-21140076" control_type="pos"
common name="xyz" value="1234"
common
product name="anything" sys_name="mhr22:21140016-21140079 act_ferq="UREWUERWERU" s_c="7"
common name="xyz" value="1234"
common
product name="anything" sys_name="mhr22:21140017-21140076" control_type="neg"
common name="xyz" value="1234"
common
我正在使用python regex来处理它,下面是我要做的(伪代码(:
filein=open('inputfile.txt','r')
fileout=open('fileout.txt','w')
for line in filein.readlines():
if re.search('product name="[^s]+"',line):
if "control_type" in line:
delete s_c="7" & act_ferq="UREWUERWERU"
and rearrange line
(#like this# product name="anything" sys_name="mhr22:21140017-21140076"
control_type="neg")
else:
rearrange line
print (line)
(#like this# #product name="anything" sys_name="mhr22:21140017-21140076
act_ferq="UREWUERWERU" s_c="7")
如果已经有人问过这种问题,我很抱歉。非常感谢。
我可以通过以下方法获得:
for line in text.splitlines():
if re.search(r'product name="[^s]+"', line):
change=line.split()
print(change[0]+' '+change[1]+' '+change[4]+' '+change[3]+' '+change[2])
print(line)
else:
print(line)