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?

4
  • 1
    you should never treat regexp as a valid tool when manipulating a markup data (xml/html) - use a proper parsers Commented Sep 8, 2019 at 19:06
  • @RomanPerekhrest, can you elaborate on it, please? Commented Sep 8, 2019 at 19:07
  • this is a well-known issue: stackoverflow.com/questions/1732348/… . Also find another links here stackoverflow.com/questions/22937618/… section "Do not use regex to parse HTML:" Commented Sep 8, 2019 at 19:16
  • OK, thank you for the links. For my purposes, it worked alright but I will keep the issues in mind. Commented Sep 8, 2019 at 20:00

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

yeah, the solution was much easy. Thank you! I will accept your reply in 11 min as per Stack's requirements.
You're welcome! It's often easier when it's someone else's code ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.