Apr-01-2022, 03:54 AM
Hello everyone, Need help with this issue. What I am seeking to do is three things.
1. convert strings that start and end with numbers to num2words
2. convert only if ends with number to num2words
3. All other numbers convert to num2words.
However the first one is currently stuck on the first item. I am trying to combine lambda instead of using multiple if statements. The following is what I have so far.
How can I solve this issue? Thanks
1. convert strings that start and end with numbers to num2words
2. convert only if ends with number to num2words
3. All other numbers convert to num2words.
However the first one is currently stuck on the first item. I am trying to combine lambda instead of using multiple if statements. The following is what I have so far.
for tx in txt: md=re.search('(?<=[0-9]. )(.*)(?=.txt)',tx) #Filter between the numbers and the extension txt #print('MD',md[0]) with open(tx, 'r+', encoding="cp1251", errors='ignore') as f: #Open text file Lines = f.readlines() text = f.read() #Read Text File for line in Lines: mda=re.search(md[0],line) if re.search(md[0],line): cap=re.search(md[0],line) chp=cap[0].strip() if chp.startswith(('1','2','3','4','5')) and chp.endswith(('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26', '27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51', '52','53','54','55','56','57','58','59','60','61','62','63','64','65','66','67','68','69','70','71','72','73','74','75','76', '77','78','79','80','81','82','83','84','85','86','87','88','89','90','91','92','93','94','95','96','97','98','99','100','101', '102','103','104','105','106','107','108','109','110','111','112','113','114','115','116','117','118','119','120','121','122','123', '124','125','126','127','128','129','130','131','132','133','134','135','136','137','138','139','140','141','142','143','144','145', '146','147','148','149','150')): #Check if starts and ends with numbers ordr= re.sub(r"^[^\d]*(\d+)", lambda x: num2words(int(x.group(1)),to='ordinal') +' ', line) #Convert to ordinal chpt2= re.sub(r"(\d+)(?!.*\d)", lambda a: ' Chapter ' + num2words(int(a.group(0))) +' ', line) #Convert and add Chapter comblam = lambda q: ordr(q) + chpt2(q) #Combine lambda print('Lets see ',comblam(2)) #Print ResultsHowever when ran I get error message of
comlam = lambda q: ordr(q) + chpt2(q) #Combine lambda TypeError: 'str' object is not callable
How can I solve this issue? Thanks