02-06-2012, 09:24 AM | #1 |
Member
Posts: 21
Karma: 10
Join Date: Jan 2012
Device: Kindle 3 & Mac OSX
|
Python help please
I am trying to modify a template function without knowing Python. I'm sure it must be easy to do, but can't get the syntax correct. Here are the details:
The function has the statement 'date = mg.group(1)' which returns a string of the format 'mm/dd/yyyy' What I want to do is to swap the order of the 'mm' & 'dd' so the final string has the form 'dd/mm/yyyy'. How do you do this in Python? Thank you for your help. Andy |
02-06-2012, 11:42 AM | #2 |
Grand Sorcerer
Posts: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
There are lots of ways to do this ranging from changing the regexp to return more groups to reconstructing the string.
One way: Code:
if mg is None: return no_page_read_str date = mg.group(1) date = "{1}/{0}/{2}".format(*date.split('/')) pct = mg.group(2) Another way, less magic: Code:
if mg is None: return no_page_read_str date = mg.group(1) parts = date,split('/') date = parts[1] + '/' + parts[0] + '/' + parts[2] pct = mg.group(2) Last edited by chaley; 02-06-2012 at 11:54 AM. Reason: Hit the post button by mistake |
Advert | |
|
02-06-2012, 03:51 PM | #3 |
Member
Posts: 21
Karma: 10
Join Date: Jan 2012
Device: Kindle 3 & Mac OSX
|
Thank you!
Hi Chaley!
I used the second example you gave as it was more intuitive to me. There is a small error in that to get it to work I changed: parts = date,split('/') to parts = date.split('/') It now works exactly as I want it to. Thank you very much... |
02-08-2012, 09:51 AM | #4 |
Member
Posts: 21
Karma: 10
Join Date: Jan 2012
Device: Kindle 3 & Mac OSX
|
After evaluating different formats I eventually settled on using:
date = 'Read ' + parts[0] + '/' + parts[2] For read books this now produces a column entry of the form "Read mm/yy" I do so like calibre! |
Tags |
python function |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Python for kindle | Matan | Kindle Developer's Corner | 8 | 01-04-2013 01:54 PM |
What is python | The Terminator | General Discussions | 20 | 01-21-2011 12:58 PM |
Python 2.7 | DiapDealer | Calibre | 4 | 12-17-2010 11:19 AM |
Python error! | didiyy | Calibre | 6 | 09-24-2010 02:05 AM |
Python 2.5 or 2.6? | itimpi | Calibre | 5 | 01-19-2009 12:48 PM |