|
|
#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 |
|
|
|
|
|
#2 |
|
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,525
Karma: 8065948
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 12:54 PM. Reason: Hit the post button by mistake |
|
|
|
| Advert | |
|
|
|
|
#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...
|
|
|
|
|
|
#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 |
| Thread Tools | Search this Thread |
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Python for kindle | Matan | Kindle Developer's Corner | 8 | 01-04-2013 02:54 PM |
| What is python | The Terminator | General Discussions | 20 | 01-21-2011 01:58 PM |
| Python 2.7 | DiapDealer | Calibre | 4 | 12-17-2010 12:19 PM |
| Python error! | didiyy | Calibre | 6 | 09-24-2010 03:05 AM |
| Python 2.5 or 2.6? | itimpi | Calibre | 5 | 01-19-2009 01:48 PM |