1

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?

martineau
  • 99,260
  • 22
  • 139
  • 249
eponkratova
  • 389
  • 4
  • 14
  • 1
    you should never treat regexp as a valid tool when manipulating a markup data (xml/html) - use a proper parsers – RomanPerekhrest Sep 08 '19 at 19:06
  • @RomanPerekhrest, can you elaborate on it, please? – eponkratova Sep 08 '19 at 19:07
  • this is a well-known issue: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags . Also find another links here https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean/22944075#22944075 section "Do not use regex to parse HTML:" – RomanPerekhrest Sep 08 '19 at 19:16
  • OK, thank you for the links. For my purposes, it worked alright but I will keep the issues in mind. – eponkratova Sep 08 '19 at 20:00

1 Answers1

3

re.sub expects the second argument (replacement) to be a string or a function.

Try casting i to a string:

completed_account = []
for i in account:
    temp = re.sub('(?<=<key_value>).*?(?=</key_value>)', str(i), text_msg, flags=re.DOTALL)
    completed_account.append(temp)
print(completed_account)
Pierre V.
  • 1,495
  • 1
  • 8
  • 14