0

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?

Matt095
  • 857
  • 3
  • 9

2 Answers2

1

Change this:
lines = fd.read().split('\n')

To this:
lines = fd.read().split('\n')[::-1]

And read more about python structures.

smnenko
  • 101
  • 4
  • Thanks I will read about that, can you think of any way to remove all the last empty lines that is always blank when writing the file `sp_ordinal_numbers_info_fixed.txt`? – Matt095 Sep 21 '22 at 07:52
  • 1
    Use strip() or rstrip() for your case – smnenko Sep 21 '22 at 07:55
1

with open('sp_ordinal_numbers_info.txt', 'r') as fd:
    lines = fd.read().split('\n')

lines.reverse()

with open('sp_ordinal_numbers_info_fixed.txt', 'a') as f:
    for line in lines:
        number, text = line.split(":")
        text = text.strip(" ")
        f.write(text + "\n" + number + "°\n\n")

If the numbers are not in sorted order, you can sort them like that:

with open('sp_ordinal_numbers_info.txt', 'r') as fd:
    lines = fd.read().split('\n')

to_sort = []
for line in lines:
    number, text = line.split(":")
    text = text.strip(" ")
    number = int(number)
    to_sort.append((number, text))

to_sort.sort(reverse=True)

with open('sp_ordinal_numbers_info_fixed.txt', 'a') as f:
    for num, text in to_sort:
        f.write(text + "\n" + str(num) + "°\n\n")
  • Thanks, can you think of any way to remove all the last empty lines that is always blank when writing the file `sp_ordinal_numbers_info_fixed.txt`? – Matt095 Sep 21 '22 at 07:52
  • 1
    Yeah, it is because you add \n\n always in the end of the line. Just not doing that in the last iteration should fix that – NiceGuySaysHi Sep 21 '22 at 07:55