08-14-2018, 01:08 PM | #1 |
Age improves with wine.
Posts: 558
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
|
Flattening unicode with unidecode() in a plugin
I want to have authors, titles etc. flattened to ASCII, and am trying to use unidecode() to do it. In IDLE, I can say:
Code:
from unidecode import unidecode print(unidecode('Philip José Farmer')) I downloaded the unidecode module and inserted it into my plugin, and at first I got this: Code:
SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xe9 in position 0: unexpected end of data The only other notable difference is that my IDLE is using Python 3.7, whereas Calibre is using 2.7. Can anyone tell me what's going on here? And is there anything already in Calibre to do the flattening, rather than having to include the whole unidecode module in my plugin? |
08-14-2018, 02:17 PM | #2 | |
Grand Sorcerer
Posts: 6,205
Karma: 16228558
Join Date: Sep 2009
Location: UK
Device: Kobo: KA1, ClaraHD, Forma, Libra2, Clara2E. PocketBook: TouchHD3
|
Quote:
from calibre.utils.filenames import ascii_text from calibre import sanitize_file_name_unicode from calibre.ebooks.oeb.polish.check.parsing import make_filename_safe I've used all of them at some point in various calibre plugins. |
|
Advert | |
|
08-14-2018, 09:53 PM | #3 |
Enthusiast
Posts: 38
Karma: 10
Join Date: Jun 2016
Location: Minnesota USA
Device: Amazon Paperwhite 3G
|
Python 2.7 has issues with unicode. This may not help, but try adding the following line to the top of your python code before any imports:
from __future__ import (unicode_literals, division, absolute_import, print_function) Note: You may only need the unicode_literals part. |
08-15-2018, 04:54 AM | #4 | |
Age improves with wine.
Posts: 558
Karma: 95229
Join Date: Nov 2014
Device: Kindle Oasis, Kobo Libra II
|
Quote:
Meanwhile a bit of pottering around in unidecode() revealed the problem -- it uses __import__() to dynamically load a conversion module for each 256-char code page, and I just needed to change the module name to "calibre_plugins.foo.unidecode.X" instead of "unidecode.X". But I'd prefer to use something which is already present in Calibre if possible, so I'll check out your suggestions. |
|
Thread Tools | Search this Thread |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Plugin] QuickPrefsEdit - Edit plugin prefs json files. | slowsmile | Plugins | 3 | 07-25-2018 08:14 PM |
Goodread Perception Expander plugin not shown on plugin list (kobo h2o) | www | KOReader | 4 | 09-28-2017 10:34 AM |
The flattening of e-book sales | Ken.Hagdal | General Discussions | 50 | 08-26-2013 04:09 PM |
Flattening the TOC hierarchy | ElMiko | Calibre | 3 | 10-18-2012 05:53 AM |
Flattening the battery to recalibrate | nimble | Sony Reader | 6 | 03-06-2010 09:28 PM |