Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rewrite_title
#1
"""
Create a function called rewrite_title, taking two arguments:
  - String, representing HTML body (full or partial)
  - String, representing a new TITLE that must be applied into the HTML

The function must:
  1. Return a new string, where the content found between <title> and
  </title> (*case insensitive*) are replaced with the function's TITLE argument.
  All other text should remain unchanged, including the <title> opening and
  closing tags. All occurances of <title> tags should be rewritten, if there is
  more than 1.
  2. The function documentation should read:
    Replace the HTML title contents with the given TITLE

HINT: Look into the re.IGNORECASE flag for case insensitivity.

rewrite_title('<html><head><TiTLe>Foo title</title></html>', 'Bar title') ->
    '<html><head><TiTLe>Bar title</title></html>'
"""

import re

def rewrite_title(*args):
    """ Replace the HTML title contents with the given TITLE """
    htmlbody = args[0]
    newtitle = args[1]
    regex = '<title>(.+?)</title>'
    pattern = re.compile(regex,re.IGNORECASE)
    title = re.findall (pattern,htmlbody)
    rehtmlbody = re.sub (title, newtitle, htmlbody)
    print rehtmlbody

hb = '<html><head><TiTLe>Foo title</title></html>'
nt = 'Bar title'
rewrite_title(hb,nt)
what am i doing wrong,why is it not working
Reply


Messages In This Thread
rewrite_title - by roadrage - Dec-05-2016, 08:26 PM
RE: rewrite_title - by nilamo - Dec-05-2016, 08:30 PM
RE: rewrite_title - by micseydel - Dec-05-2016, 08:32 PM
RE: rewrite_title - by roadrage - Dec-05-2016, 09:30 PM
RE: rewrite_title - by nilamo - Dec-05-2016, 10:44 PM
RE: rewrite_title - by micseydel - Dec-06-2016, 08:42 PM
RE: rewrite_title - by roadrage - Dec-07-2016, 06:32 PM
RE: rewrite_title - by nilamo - Dec-07-2016, 06:50 PM
RE: rewrite_title - by roadrage - Dec-07-2016, 07:31 PM
RE: rewrite_title - by Ofnuts - Dec-08-2016, 10:16 PM
RE: rewrite_title - by roadrage - Dec-21-2016, 04:21 PM
RE: rewrite_title - by snippsat - Dec-21-2016, 05:14 PM
RE: rewrite_title - by roadrage - Dec-21-2016, 06:47 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020