Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 02-06-2012, 09:24 AM   #1
AndyW1691
Member
AndyW1691 began at the beginning.
 
AndyW1691's Avatar
 
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
AndyW1691 is offline   Reply With Quote
Old 02-06-2012, 11:42 AM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
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)
EDIT: The *date stuff unpacks the list (array) into positional parameters as required by the "format" method. It is explained in the python docs here.

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
chaley is offline   Reply With Quote
Advert
Old 02-06-2012, 03:51 PM   #3
AndyW1691
Member
AndyW1691 began at the beginning.
 
AndyW1691's Avatar
 
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...

AndyW1691 is offline   Reply With Quote
Old 02-08-2012, 09:51 AM   #4
AndyW1691
Member
AndyW1691 began at the beginning.
 
AndyW1691's Avatar
 
Posts: 21
Karma: 10
Join Date: Jan 2012
Device: Kindle 3 & Mac OSX
Thumbs up

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!
AndyW1691 is offline   Reply With Quote
Reply

Tags
python function


Forum Jump

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


All times are GMT -4. The time now is 11:59 PM.


MobileRead.com is a privately owned, operated and funded community.