Generated output in doc .txt

0

I'm still struggling with my work on text correction. It turns out that I have to store the output of my code in different text files that I also have to create.

my code is this:

cadena = "Bienvenido/a" 
print(cadena.center(50, "="))

entrada = input('Introduzca aquí el nombre completo del texto a analizar en formato << .txt >>:  ')
while entrada == '':
    print('Ha habido un error al encontrar su fichero, por favor, inténtelo de nuevo')
    entrada = input('Introduzca aquí el nombre completo del texto a analizar:  ')

archivo = open(entrada, 'r')
texto = archivo.read()

def contar_palabras(texto):
    count = 0
    cc = ' '
    for c in texto:
        if c == ' ' and cc != ' ':
            count += 1
        cc = c
    if c != ' ':
        count += 1

    return count

def contar_vocales(texto):
    count = 0
    for c in texto:
        if c in "aeiouAEIOU":
            count = count + 1
    return count

def contar_consonantes(texto):
    count = 0
    for c in texto:
        if c in "bcdfghjklmnñpqrstvwxyzBCDFGHJKLMNÑPQRSTVWXYZ":
            count = count + 1
    return count

def contar_mayusculas(texto):
    count = 0
    for c in texto:
        if c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
            count = count + 1
    return count

def contar_minusculas(texto):
    count = 0
    for c in texto:
        if c in "abcdefghijklmnopqrstuvwxyz":
            count = count + 1
    return count

def contar_numeros(texto):
    count = 0
    for c in texto:
        if c in "0123456789":
            count = count+  1
    return count           

def contar_diferentes(texto):
    count = 0
    for c in texto:
        if c in "!#$%&\'()*+,-./:;<=>?@[\]^_'{|}~\t\s\x0b\x0c":
            count = count + 1
    return count

def contar_parrafos(texto):  #aqui tengo que afinar mas, estoy en ello
    count = 0
    letter = ''
    for c in texto:
        if c in "\n\r":
            count = count + 1
    return count           

THE DEPARTURE OF THIS CODE I HAVE TO SAVE IN A FILE .TXT

THIS FOLLOWING VARIABLE 'CORRECTIONS' WOULD HAVE TO RECORD ME THE RESULT IN A DOC.TXT

def correcciones(libro):
    modif = []
    for c in texto:
        tabs = 0
        while c.startswith("   "):
            tabs+=1
            c = c[4:]
        c = "\t"*tabs + c

        c = re.sub(r"(\S) {2,}", r" ", c)

        c = re.sub(r"([:;.,]+)(\S)", r" ", c)

        modif.append(c)

    return modif

A thousand thanks !!!!!

    
asked by Eva Rentero 04.01.2019 в 15:08
source

1 answer

0

Put this at the end of your code:

def guardarArchivo(valor):
  # Abro (o creo) el archivo de salida en modo anexar, para agregar al final del archivo.
  with open("salida.txt","a") as archivo:
    if type(valor) is list:
      # Si el valor recibido es de tipo lista, uso writelines, para escribir todo de una vez
      archivo.writelines("\n".join(valor))
    else:
      # Si no, uso write, para escribir sólo el valor.
      archivo.write("\n" + str(valor))

corregido = correcciones(texto)
guardarArchivo(corregido)

dif = contar_diferentes(texto)
guardarArchivo(dif)
    
answered by 04.01.2019 в 17:09