View Single Post
Old 04-11-2011, 03:52 PM   #2
Starson17
Wizard
Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.
 
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
Quote:
Originally Posted by thczv View Post
I am trying to figure out the syntax for this. But the going is slow because I don't really know what I am doing and am figuring it out by trial and error. What I need to do is insert "v-print/" within a url. For example, here is a typical article from the Sacramento Bee rss feed:

http://www.sacbee.com/2011/04/09/354...i_rss=Business

I need to automatically insert "v-print/" like so:

http://www.sacbee.com/2011/04/09/v-p...education.html

It doesn't appear to matter whether "#mi_rss=Business" appears at the end.

The url includes the date, so a simple replace won't work. I tried doing it with wildcards, but it hasn't worked so far.

Do any of you know a simple way to do this?

Thanks.
The are only a few ways this job (URL modification) is usually done in recipes. They are: 1) replace, 2) partition/rpartition, and 3) split/join.

Use replace whenever you are replacing some part of the string (usually in the middle) with some other string and the 1) part being replaced never changes (this lets you find it) and 2) you don't want to change the two parts on either side of the part being replaced. Replace opens up the string into two parts one either side of the part being replaced. You can keep the part you are replacing (by inserting itself back in) and you can add additional stuff in the middle, but you can't change the first and last parts.

Use partition/rpartition when you want to split the string into three parts and the part in the middle never changes (so you can find it), but you want to change one or more of the three parts.

If there isn't any unchanging part in the middle for you to find (as in your case) then the solution most commonly used is split/join with splitting being done on the slash. It splits the URL into each part between slashes and you can do whatever you want to each piece, then put them together. You find the part to change by counting the cut up pieces (between slashes) and changing the pieces you need to change.

Try this:
Code:
    def print_version(self,url):
        segments = url.split('/')
        printURL = '/'.join(segments[0:6]) + '/v-print/' + '/'.join(segments[6:])
        return printURL

Last edited by Starson17; 05-05-2011 at 09:16 AM.
Starson17 is offline   Reply With Quote