[Python] How can I extract numerator and denominator from a list w/ python?

Discussão em 'Python' iniciado por Stack, Junho 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have this file with a list of fractions and I want to simplify them. The list is written like follows:

    124/246
    71/69
    2574/3850
    ...


    In order to simplify im trying to extract numerator and denominator with the following code but it only seems to be extracting the last digits(f1 and f2 are my opened files where I read and write, and Simplify is a previously defined function):

    while f1.readline() != "":
    i = 1
    while f1.read(1) != "/":
    num = f1.read(i)
    i += 1

    den = f1.read(i)
    num, den = Simplify(int(num), int(den))

    f2.write(str(num) + "/" + str(den) + "\n")


    I would appriciate some guidance. Thanks a lot :)

    Continue reading...

Compartilhe esta Página