Formatting a python stored text

Asked

Viewed 912 times

2

I made a random number generator that generates and saves in.txt file

I made generate 3 combinations, it happens that when I open the txt the combinations are on the side of the other, as I do to be below each other?

import random

c1 = (random.choice([9, 9]))
c2 = (random.choice([1, 1]))
c3 = (random.choice([9, 9, 9]))
lista = []


def gerar_randomico():
    return random.choice([2, 3, 4, 5, 6, 7, 8, 9])


def gerar_randomicus():
    return random.choice([6, 7, 8, 9])


for i in range(3):
    lista1 = ('{}{}{}{}{}{}{}{}{}{}{}'.format(c1, c2, c3, gerar_randomicus(), gerar_randomico(), gerar_randomico(),
                                              gerar_randomico(), gerar_randomico(), gerar_randomico(),
                                              gerar_randomico(), gerar_randomico()))
    with open('arquivo.txt','a') as arquivo:
        arquivo.write(str(lista1))
        lista_guardar = lista.append(arquivo)
        print(lista_guardar)

1 answer

1

Just add the line break when writing to the file:

arquivo.write(str(lista1) + '\n')

Incidentally, lista1 is already a string, then call str is unnecessary.

arquivo.write(lista1 + '\n')

You also put the context manager that handles the file within the loop loop; this means that your file will be open, written, and closed at each iteration. Not very usual. It would be better if you open the file only once and keep it open while writing:

with open('arquivo.txt', 'a') as arquivo:
    for i in range(3):
        ...
        arquivo.write(lista1 + '\n')

How much to format the string, you do not need to repeat the function call so many times. You can generate a list with these random values and use the deconstruction of lists:

numeros_aleatorios = [gerar_randomico() for _ in range(7)]
lista1 = '{}{}{}{}{}{}{}{}{}{}{}'.format(c1, c2, c3, gerar_randomicus(), *numeros_aleatorios)

But, better than that, you can use the function random.choices to return a list of random numbers:

import random

def gerar_randomico(n=1):
    return random.choices([2, 3, 4, 5, 6, 7, 8, 9], k=n)

def gerar_randomicus(n=1):
    return random.choices([6, 7, 8, 9], k=n)

c1 = random.choice([9, 9])
c2 = random.choice([1, 1])
c3 = random.choice([9, 9, 9])

lista = []

with open('arquivo.txt','a') as arquivo:
    for i in range(3):
        valores = [c1, c2, c3, *gerar_randomicus(n=1), *gerar_randomico(n=7)]
        lista1 = ''.join(str(valor) for valor in valores)

        arquivo.write(lista1 + '\n')

I also noticed that you did:

...
lista_guardar = lista.append(arquivo)

This didn’t make much sense. Why would you add in the list several times the object arquivo? As well as the method append the list has no return, so it makes no sense to assign it to a variable.

Other details that didn’t make sense:

  • c1 = random.choice([9, 9]), c1 will always be 9;
  • c2 = random.choice([1, 1]), c2 will always be 1;
  • c3 = random.choice([9, 9, 9]), c3 will always be 9;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.