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 09-28-2024, 07:09 AM   #1
Wiggo
Leftutti
Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.
 
Wiggo's Avatar
 
Posts: 486
Karma: 1153449
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
Search and Replace with Regex

I have a column (#startdates) with comma separated text, like tags, with a date in the following format: 2024.09.21

I want to insert this date into a date column (#dateread) with the format 'dd MMM yyyy'.

Could someone please help me?

Edit: Or does it work easier with action chain and a template?

Click image for larger version

Name:	calibre_hQvIsRlccy.jpg
Views:	85
Size:	160.3 KB
ID:	211076

Last edited by Wiggo; 09-28-2024 at 07:13 AM.
Wiggo is offline   Reply With Quote
Old 09-28-2024, 12:39 PM   #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: 12,014
Karma: 7257321
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Wiggo View Post
I have a column (#startdates) with comma separated text, like tags, with a date in the following format: 2024.09.21

I want to insert this date into a date column (#dateread) with the format 'dd MMM yyyy'.

Could someone please help me?

Edit: Or does it work easier with action chain and a template?

Attachment 211076
Is the date always the first value in #startdates? The only value? The last value? If not, how do you know which value to use?

If it is the first or only value, then I suggest you use a search & replace using "Template" as the source. The template would be
Code:
program: format_date(sublist(#startdates, 0, 1, ','), 'dd MMM yyyy')
If it is the last value then this should work:
Code:
program: format_date(sublist(#startdates, -1, 0, ','), 'dd MMM yyyy')

The search & replace dialog contents:
  • Search mode: Regular expression
  • Search field: {template}
  • Template: as above
  • Search for: ^(.*)$
  • Replace with: \1
  • Destination field: #dateread
If the date is buried somewhere in a multi-valued column then things are more complicated. You will need to search in the template for the date probably using a for loop, processing the first date you find.

As always, be careful when using search & replace. Try it on a few books before running it on your entire library. Backing up your library is a good plan.
chaley is offline   Reply With Quote
Advert
Old 09-28-2024, 01:08 PM   #3
Wiggo
Leftutti
Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.
 
Wiggo's Avatar
 
Posts: 486
Karma: 1153449
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
Thank you so much, chaley!

Unfortunately I get this error

Code:
calibre, version 7.19.0
FEHLER: Fehlgeschlagen: Unknown string format: S/R-Vorlagenfehler Formatter: Failed to scan program. Invalid input '#startdates, 0, 1, ','), 'dd MMM yyyy')' near the end of the program

Traceback (most recent call last):
  File "calibre\gui2\dialogs\metadata_bulk.py", line 198, in do_it
  File "calibre\gui2\dialogs\metadata_bulk.py", line 535, in do_all
  File "calibre\db\cache.py", line 85, in call_func_with_lock
  File "calibre\db\cache.py", line 1636, in set_field
  File "calibre\db\write.py", line 568, in set_books
  File "calibre\db\write.py", line 568, in <dictcomp>
  File "calibre\db\write.py", line 73, in adapt_datetime
  File "calibre\utils\date.py", line 114, in parse_date
  File "dateutil\parser\_parser.py", line 1368, in parse
  File "dateutil\parser\_parser.py", line 643, in parse
dateutil.parser._parser.ParserError: Unknown string format: S/R-Vorlagenfehler Formatter: Failed to scan program. Invalid input '#startdates, 0, 1, ','), 'dd MMM yyyy')' near the end of the program
Click image for larger version

Name:	calibre_R2HuIBOkeg.jpg
Views:	32
Size:	220.2 KB
ID:	211092
Wiggo is offline   Reply With Quote
Old 09-28-2024, 01:13 PM   #4
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: 12,014
Karma: 7257321
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Sorry, I forgot the '$' when I replaced my test lookup name with your lookup name. It should be
Code:
program: format_date(sublist($#startdates, 0, 1, ','), 'dd MMM yyyy')
chaley is offline   Reply With Quote
Old 09-28-2024, 01:37 PM   #5
Wiggo
Leftutti
Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.
 
Wiggo's Avatar
 
Posts: 486
Karma: 1153449
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
Quote:
Originally Posted by chaley View Post
Sorry, I forgot the '$' when I replaced my test lookup name with your lookup name. It should be
Code:
program: format_date(sublist($#startdates, 0, 1, ','), 'dd MMM yyyy')
It works!

But I get this error message for 'Dezember' - language thing?

Code:
calibre, version 7.19.0
FEHLER: Fehlgeschlagen: Unknown string format: 30 Dez 2023

Traceback (most recent call last):
  File "calibre\gui2\dialogs\metadata_bulk.py", line 198, in do_it
  File "calibre\gui2\dialogs\metadata_bulk.py", line 535, in do_all
  File "calibre\db\cache.py", line 85, in call_func_with_lock
  File "calibre\db\cache.py", line 1636, in set_field
  File "calibre\db\write.py", line 568, in set_books
  File "calibre\db\write.py", line 568, in <dictcomp>
  File "calibre\db\write.py", line 73, in adapt_datetime
  File "calibre\utils\date.py", line 114, in parse_date
  File "dateutil\parser\_parser.py", line 1368, in parse
  File "dateutil\parser\_parser.py", line 643, in parse
dateutil.parser._parser.ParserError: Unknown string format: 30 Dez 2023
Edit: Looks loke a language thing. When I switch calibre to English, everything works.

Last edited by Wiggo; 09-28-2024 at 01:45 PM.
Wiggo is offline   Reply With Quote
Advert
Old 09-28-2024, 01:49 PM   #6
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: 12,014
Karma: 7257321
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Wiggo View Post
But I get this error message for December, but the tag is still written - language thing?

Code:
calibre, version 7.19.0
FEHLER: Fehlgeschlagen: Unknown string format: 30 Dez 2023
Yes, it is a language problem. Calibre is trying to parse the date string to store it in the database and doesn't know what "Dez" means.

The right way to fix this is to change (if necessary) the definition of the #dateread column to have the format you want, 'dd MMM yyyy'. Once you do that then you don't need the format_date() at all. Simply copy the date from #startdates to #dateread as it is. Calibre will convert it to a date then display it using the format you specify in the column definition.

In this case the template becomes
Code:
program: sublist($#startdates, 0, 1, ',')
You can do it without using template mode if the date you want is the only value in #startdates.
chaley is offline   Reply With Quote
Old 09-28-2024, 02:01 PM   #7
Wiggo
Leftutti
Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.
 
Wiggo's Avatar
 
Posts: 486
Karma: 1153449
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
Quote:
Originally Posted by chaley View Post
Yes, it is a language problem. Calibre is trying to parse the date string to store it in the database and doesn't know what "Dez" means.

The right way to fix this is to change (if necessary) the definition of the #dateread column to have the format you want, 'dd MMM yyyy'. Once you do that then you don't need the format_date() at all. Simply copy the date from #startdates to #dateread as it is. Calibre will convert it to a date then display it using the format you specify in the column definition.

In this case the template becomes
Code:
program: sublist($#startdates, 0, 1, ',')
You can do it without using template mode if the date you want is the only value in #startdates.
It works again! Fortunately my column was already defined with 'dd MMM yyyy'.

Wiggo is offline   Reply With Quote
Old 09-29-2024, 12:50 AM   #8
Wiggo
Leftutti
Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.
 
Wiggo's Avatar
 
Posts: 486
Karma: 1153449
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
One more question please, chaley.

The timestamp has the time 00:00:00 CEST. Can this be changed?
When daylight saving time is changed again, all dates have a timeshift of -1 day, which admittedly annoys me a bit.
Wiggo is offline   Reply With Quote
Old 09-29-2024, 05:38 AM   #9
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: 12,014
Karma: 7257321
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Wiggo View Post
One more question please, chaley.

The timestamp has the time 00:00:00 CEST. Can this be changed?
When daylight saving time is changed again, all dates have a timeshift of -1 day, which admittedly annoys me a bit.
Use date_arithmetic() to add the number of hours you want. Example: to add 12 hours to the date making it noon, use:
Code:
program: date_arithmetic(sublist($#startdates, 0, 1, ','), '12h')
chaley is offline   Reply With Quote
Old 09-29-2024, 07:51 AM   #10
Wiggo
Leftutti
Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.
 
Wiggo's Avatar
 
Posts: 486
Karma: 1153449
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
Thx, chaley!
Wiggo is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to replace text with Search and Replace with regex on Calibre darrnih ePub 2 04-02-2024 02:10 AM
Regex in search problems (NOT Search&Replace; the search bar) lairdb Calibre 3 03-15-2017 07:10 PM
Regex: Search and Replace Thomas_AR Calibre 2 03-31-2016 06:23 PM
regex search/replace - how to? Alt68er Sigil 1 03-11-2014 08:53 PM
need regex help search and replace schuster Calibre 4 01-10-2011 09:00 AM


All times are GMT -4. The time now is 10:49 AM.


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