Changing the attributes postion in a tag using soup/python

This is the place for queries that don't fit in any of the other categories.

Changing the attributes postion in a tag using soup/python

Postby mallik9292 » Thu Jul 31, 2014 1:33 pm

Hi

I am newbie to python . I am using soup for parsing the xml and place the missing tags at particular places. I am having couple of doubts

1. I want to replace the attributes position in a tag . For example

<pub-date date-type="cover" pub-type="print" iso-8601-date="2005">
This tag having attributes as "date-type" and "iso-8601-date"

i want the output as

<pub-date iso-8601-date="2005" pub-type="print" date-type="cover">

I have tried the following code which i am able to replace only the first tag but not the second one.
Code: Select all
dte = "date-type=\"cover\""
   iso = "iso-8601-date=\"2005"
   links = soup.find("pub-date")
   strToSearch = '<pub-date date-type="cover" pub-type="print" iso-8601-date="^2">'
   for link in links :
      dtype = strToSearch.find(dte)
      istype = strToSearch.find(iso)
      step_1 = strToSearch.replace(iso,' ')
      temp = step_1.replace(dte,iso)


please help me resolving this. Thanks in advance
mallik9292
 
Posts: 1
Joined: Thu Jul 31, 2014 1:23 pm

Return to General Coding Help

Who is online

Users browsing this forum: myhy, Yoriz and 4 guests

cron