2

I'm trying to replace a string stored in a list with an HTML tag in a file by doing:

    links=[<a href="http://hexagon-dashboard-gbc-01/vboard/latest?regs=3281546">http://hexagon-dashboard-gbc-01/vboard/latest?regs=3281546<!--V68NUR--></a>]
   str1="""<a href="%s">%s<!--V68NUR--></a>"""%(vboard['V68N']['perf.tl'],vboard['V68N']['perf.tl'])
     with open(html_file,'r+') as f:
                content = f.read()
                f.seek(0)
                f.truncate()
                f.write(content.replace(links[0],str1))
                

But I get the following error:

TypeError: replace() argument 1 must be str, not Tag.

What am I missing? Please help me with the modification I have to do.

1 Answer 1

2

Updated:

From what you posted, I suppose you are treating a html file as plain text and going to perform string replacement.

The replace() function only works when both of its arguments are strings.

The reason you got an error is that links[0] is not a string but a tag.

If you manage to get links like this (note the single quotes)

links=['<a href="http://hexagon-dashboard-gbc-01/vboard/latest?regs=3281546">http://hexagon-dashboard-gbc-01/vboard/latest?regs=3281546<!--V68NUR--></a>']

then

content.replace(links[0],str1)

would not produce any errors.

To edit html files, you can also use HTML Parser instead.

Sign up to request clarification or add additional context in comments.

2 Comments

links is also obtained from html
Thanks@Steven Liang

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.