I have a list of accounts:
account = [79173,84830,86279]
I have a string which contains n information, including an account id between <key_value>
and </key_value>
:
import re
text_msg = """
<field>
<field_name>accountinfoid2</field_name>
<key_value>286249</key_value>
<target_name>Field2</target_name>
<target_type>Integer</target_type>
<target_format />
<target_length>-1</target_length>
<target_precision>-1</target_precision>
<target_decimal_symbol />
<target_grouping_symbol />
<target_currency_symbol />
<target_null_string />
<target_aggregation_type>-</target_aggregation_type>
</field>
"""
What I need is to replace 286249
with account id from the list account
:
<field>
<field_name>accountinfoid2</field_name>
<key_value>79173</key_value>
<target_name>Field2</target_name>
<target_type>Integer</target_type>
<target_format />
<target_length>-1</target_length>
<target_precision>-1</target_precision>
<target_decimal_symbol />
<target_grouping_symbol />
<target_currency_symbol />
<target_null_string />
<target_aggregation_type>-</target_aggregation_type>
</field>
<field>
<field_name>accountinfoid2</field_name>
<key_value>84830</key_value>
<target_name>Field2</target_name>
<target_type>Integer</target_type>
<target_format />
<target_length>-1</target_length>
<target_precision>-1</target_precision>
<target_decimal_symbol />
<target_grouping_symbol />
<target_currency_symbol />
<target_null_string />
<target_aggregation_type>-</target_aggregation_type>
</field>
<field>
<field_name>accountinfoid2</field_name>
<key_value>86279</key_value>
<target_name>Field2</target_name>
<target_type>Integer</target_type>
<target_format />
<target_length>-1</target_length>
<target_precision>-1</target_precision>
<target_decimal_symbol />
<target_grouping_symbol />
<target_currency_symbol />
<target_null_string />
<target_aggregation_type>-</target_aggregation_type>
</field>
I've tried:
completed_account = []
for i in account:
temp = re.sub('(?<=<key_value>).*?(?=</key_value>)',i,text_msg,flags=re.DOTALL)
completed_account.append(temp)
print(completed_account)
and got the error:
decoding to str: need a bytes-like object, int found
What am I doing wrong here?