enter image description here

There is a txt file with dates of birth of certain persons. enter image description here

I need to extract only the dates of birth, that is, the numbers in the format 'ymd', but why in the list of data all data are presented in the form of 'y.m', '.d'? how to fix, please tell me!

    1 answer 1

    As you can see, all the examples are without regulars, as they say in a famous meme: if you decide to solve a problem with regular expressions, then you have 2 problems. xD

    in_str = ''' А Б 2000.1.9 В Г 2001.6.7 Д Е 2000.7.5 Ж З 2005.9.4 И К 1999.6.4 '''; # in_list содержит строки из файла, для удобства я сделал через in_str in_list = list(filter(lambda x: x != '', in_str.split("\n"))); out_list = []; for string in in_list: item = string.split(' ')[2].split('.'); out_list.append(item); print(out_list); 

    Conclusion:

     [['2000', '1', '9'], ['2001', '6', '7'], ['2000', '7', '5'], ['2005', '9', '4'], ['1999', '6', '4']] 

    UPD0: So that the list contains dates, not strings , you can do this:

     in_str = ''' А Б 2000.1.9 В Г 2001.6.7 Д Е 2000.7.5 Ж З 2005.9.4 И К 1999.6.4 '''; # in_list содержит строки из файла, для удобства я сделал через in_str in_list = list(filter(lambda x: x != '', in_str.split("\n"))); out_list = []; for string in in_list: item = list(map(int, string.split(' ')[2].split('.'))); out_list.append(item); print(out_list); 

    Conclusion:

     [[2000, 1, 9], [2001, 6, 7], [2000, 7, 5], [2005, 9, 4], [1999, 6, 4]] 

    UPD1: It seems I didn’t quite understand what was needed, if you just need a list of dates in the form of strings, you can:

     in_str = ''' А Б 2000.1.9 В Г 2001.6.7 Д Е 2000.7.5 Ж З 2005.9.4 И К 1999.6.4 '''; # in_list содержит строки из файла, для удобства я сделал через in_str in_list = list(filter(lambda x: x != '', in_str.split("\n"))); out_list = []; for string in in_list: item = string.split(' ')[2]; out_list.append(item); print(out_list); 

    Conclusion:

     ['2000.1.9', '2001.6.7', '2000.7.5', '2005.9.4', '1999.6.4']