![]() |
#1 |
Junior Member
![]() Posts: 8
Karma: 10
Join Date: Mar 2011
Device: Kindle
|
SacBee print_version syntax
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. |
![]() |
![]() |
![]() |
#2 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Quote:
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. |
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Junior Member
![]() Posts: 8
Karma: 10
Join Date: Mar 2011
Device: Kindle
|
This appears to work great. Thank you. Could you explain the nuts and bolts logic of what this does, or direct me to a web site where I can learn about it?
Thanks again. |
![]() |
![]() |
![]() |
#4 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Quote:
Code:
def print_version(self,url): #split will chop up the URL into a Python list of string pieces #each one was a string between a slash segments = url.split('/') #segments is now the name of that list of strings printURL = '/'.join(segments[0:6]) + '/v-print/' + '/'.join(segments[6:]) #segments[0:6] is a list containing the first six string pieces. # '/'.join(segments[0:6]) is a single string where the first six strings #have been concatenated back as a single string, with the slashes put back # segments[6:] is a list of all string pieces after the sixth # see above for what '/'.join(segments[6:]) is # The + signs concatenate it together, putting '/v-print/' in the middle with the slashes return printURL |
|
![]() |
![]() |
![]() |
#5 |
Junior Member
![]() Posts: 8
Karma: 10
Join Date: Mar 2011
Device: Kindle
|
You did go above and beyond. I really appreciate the additional explanation.
Thanks. |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Member
![]() Posts: 10
Karma: 10
Join Date: Apr 2011
Device: Kindle
|
sorry to hijack the thread! but this is so helpful! thanks Starson17 and to the OP
|
![]() |
![]() |
![]() |
#7 | |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Quote:
I wrote a big chunk of it, but before it was ready, a lot of my links to relevant material changed when Kovid switched the bug tracker and rewrote/reorganized some of the User Guide. I also decided I was repeating too much of what Kovid had already written and decided I needed to focus even more on links, not repeating things. I've started over from the beginning. @audreypots You aren't hijacking. I wrote the answer for people like you - hoping to explain how this problem is usually solved and to encourage others to join in and write a few recipes. Thanks for posting that you found it helpful. Last edited by Starson17; 04-12-2011 at 09:43 AM. |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Markdown Syntax in Comments | itimpi | Calibre | 11 | 01-25-2011 11:28 AM |
Removing header syntax. | boromirofborg | Calibre | 0 | 07-21-2010 12:33 AM |
Metadata Filename Syntax | gandor62 | Calibre | 15 | 07-18-2010 03:46 AM |
Invalid Syntax Error | msprang | Sony Reader | 7 | 11-07-2009 01:11 PM |
Using PubDate in print_version of custom news source | mobilereader72 | Calibre | 4 | 05-30-2009 05:52 PM |