![]() |
#1 |
Morlock
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 34
Karma: 2734796
Join Date: Oct 2021
Device: Kindle Paperwhite
|
Roman to Arabic
I found this regex function on this forum and it worked at the time. Now it works the first time, and every time I open the edit function window, even without making changes, and thereafter returns 0. I'm not enough of a coder to know why. Could anyone please help?
The originally posted function had n = unicode(n).upper(), which I changed to n = str(n).upper() in order to make it work then. I understand that that was because of changes to Python. Could it be that Python has changed again? Code:
numeral_map = zip( (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1), ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I') ) def arabic_num(n): n = str(n).upper() i = result = 0 for integer, numeral in numeral_map: while n[i:i + len(numeral)] == numeral: result += integer i += len(numeral) return result def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): return match.group(1)+str(arabic_num(match.group(2)))+match.group(3) |
![]() |
![]() |
![]() |
#2 |
Morlock
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 34
Karma: 2734796
Join Date: Oct 2021
Device: Kindle Paperwhite
|
I found the answer to my question, thanks to AI.
The lines: numeral_map = zip( (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1), ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I') ) Needed to be replaced with this: numeral_map = list(zip( (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1), ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I') )) The function is working again and saving lots of fiddly work. |
![]() |
![]() |
Advert | |
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
i need german:arabic and french arabic and italian arabic | takfarinas | Kobo Reader | 3 | 02-14-2021 07:12 AM |
Arabic to Arabic dictionary for Kobo | hapaahamza | Kobo Reader | 1 | 09-04-2019 03:58 PM |
Convert Roman numerals to Arabic? | Peter W | Sigil | 2 | 04-09-2012 11:55 AM |
PRS-900 Times New Roman and PDF | vxf | Sony Reader | 2 | 06-18-2010 09:02 AM |
Non-roman fonts | ebookworm | Sony Reader | 6 | 10-06-2007 12:12 AM |