Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read a txt file from the last of its lines to the first of its lines?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :


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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading