<fireparams>
<fire>
<param name="extra_ammo" value="120" />
<param name="ammo_type" value="bullet_ar" />
<param name="bullet_chamber" value="1" />
<param name="helper_tp" value="weapon_term" />
<param name="helper_fp" value="weapon_term" />
<param name="nearmiss_signal" value="OnNearMiss" />
</fire>
</fireparams>
_path = 'xml/' # путь к папке с xml файлами
def parse_xml(filename):
with open(_path+filename) as file:
xml = file.read()
soup = BeautifulSoup(xml,'html.parser')
params = soup.select('param')
my_list = []
for x in params:
my_list.append(x['name']+'='+x['value'])
return '|'.join(my_list)
file_list = os.listdir(_path)
file = open('newfile.txt','w')
for filename in file_list:
file.writelines(parse_xml(filename)+'\n')
file.close()
extra_ammo=120|ammo_type=bullet_ar|bullet_chamber=1|helper_tp=weapon_term|helper_fp=weapon_term|nearmiss_signal=OnNearMiss
$ cat file.xml | awk -F'=|"' '/=/{print $3"="$6}' | tr '\n' '|' | sed 's/.$//'
$ cat file.xml | xq -r '.fireparams.fire.param[] | "\(."@name")=\(."@value")"' | tr '\n' '|' | sed 's/.$//'
data = '''<fireparams>
<fire>
<param name="extra_ammo" value="120" />
<param name="ammo_type" value="bullet_ar" />
<param name="bullet_chamber" value="1" />
<param name="helper_tp" value="weapon_term" />
<param name="helper_fp" value="weapon_term" />
<param name="nearmiss_signal" value="OnNearMiss" />
</fire>
</fireparams>'''
import xml.etree.ElementTree as ET
print('|'.join([f"{x.get('name')}={x.get('value')}" for x in ET.fromstring(data)[0]]))
extra_ammo=120|ammo_type=bullet_ar|bullet_chamber=1|helper_tp=weapon_term|helper_fp=weapon_term|nearmiss_signal=OnNearMiss
<fireparams> и <fire>
:<fireparams>\r\n<fire>\r\n|</fire>\r\n</fireparams>\r\n
<param name="(.*)" value="(.*)" />\r\n
\1=\2|