I need to read this file with a python code but in an inverted way to obtain the output that I will indicate below.
sp_ordinal_numbers_info.txt
1: Primero
2: Segundo
3: Tercero
4: Cuarto
5: Quinto
10: Décimo
715: Septuagentésimo décimo quinto
This is the code that will read the text file, modify it line by line, and then write it modified line by line into a new text file.
import re
from unicodedata import normalize
symbolic_ordinal_number, colloquial_ordinal_number = "", ""
with open('sp_ordinal_numbers_info.txt', 'r') as fd:
lines = fd.read().split('\n')
for i, line in enumerate(lines):
line = line.lower()
line = re.sub(r"([^n\u0300-\u036f]|n(?!\u0303(?![\u0300-\u036f])))[\u0300-\u036f]+", r"\1", normalize("NFD", line), 0, re.I)
line = normalize('NFC', line) # -> NFC
print(line)
m1 = re.search(r"((?:\w\s*)+)[\s|]*:[\s|]*((?:\w\s*)+)", line, re.IGNORECASE)
if m1:
symbolic_ordinal_number, colloquial_ordinal_number = m1.groups()
with open('sp_ordinal_numbers_info_fixed.txt', 'a') as f:
f.write(colloquial_ordinal_number + "\n" + symbolic_ordinal_number + "º" + "\n\n")
This is the inverted input that I get with my code, but the problem is that the ordinal numbers are from least to greatest and I need to have them from greatest to least, to which I wonder if there is a way to write them already in the correct order
sp_ordinal_numbers_info_fixed.txt
primero
1º
segundo
2º
tercero
3º
cuarto
4º
quinto
5º
decimo
10º
Septuagentésimo décimo quinto
715º
And this is the correct output that I need obtain:
sp_ordinal_numbers_info_fixed.txt
Septuagentésimo décimo quinto
715º
decimo
10º
quinto
5º
cuarto
4º
tercero
3º
segundo
2º
primero
1º
If possible I would like to read the sp_ordinal_numbers_info.txt
file from its last line to the end of the first line without using libraries like os
Another problem I found is that at the end of the generated file sp_ordinal_numbers_info_fixed.txt
I always have an unwanted last empty line, is there a way to remove that last empty line?