Save the contents of a file in another file - python

0

I'm trying to read information from a file and save it in a new file after passing all the information to lowercase and removing punctuation characters. For this I created a ReadFile () function that reads the file in question and saves it to a global variable, date. After reading the file and storing the information in the data variable it will move to lowercase and pull out the special characters through the PunctuactionLowercase () function. And finally, I wanted to save this new information in a new file, however I'm not able to. Can someone help me?

import string
data=[]
dataLower=[]
dataPunctuation=[]

def ReadFile():
    global data
    f=open("chap.1_esp.txt",'r')
    while True:
        line=f.readline()        
        if not line:
            break
        data.append(line)

    f.close()
    for i in range(len(data)):
        print(data[i],end=" ")

def PunctuationLowercase():
    global data, dataLower, dataPunctuation
    translator=str.maketrans('','',string.punctuation)
    for i in range(len(data)):
        dataLower.append(data[i].lower())   
        dataPunctuation.append(dataLower[i].translate(translator)) 
        print(dataPunctuation[i],end=" ") 

def SaveFile():
    global data, dataLower, dataPunctuation

    texto="Isto é uma variável que vai ser guardada num ficheiro."
    with open("chap.1_new.txt","w") as f:
        f.write(texto)   
        f.write("Isto vai ser guardado num ficheiro.")
        for i in range(len(data)):
                f.write(dataPunctuation.append(dataLower[i])

    f.close()


if __name__ == "__main__":
    ReadFile()
    PunctuationLowercase()
    SaveFile()
    
asked by anonymous 10.12.2018 / 15:54

1 answer

0

Hello! I believe you need to use the global variables in a different way and another point would be with regard to writing to the file (each with your responsibility). follow code running:

import string

def ReadFile():
    global data
    f=open("chap.1_esp.txt",'r')
    data = f.read()
    while True:
        line=f.readline()        
        if not line:
            break
        data.append(line)

    f.close()
    for i in range(len(data)):
        print(data[i],end=" ")

def PunctuationLowercase():
    global dataLower
    global dataPunctuation
    dataLower = []
    dataPunctuation = []
    translator=str.maketrans('','', string.punctuation)
    for i in range(len(data)):
        dataLower.append(data[i].lower())   
        dataPunctuation.append(dataLower[i].translate(translator)) 
        print(dataPunctuation[i],end=" ") 

def SaveFile():
    texto="Isto é uma variável que vai ser guardada num ficheiro."
    with open("chap.1_new.txt","w") as f:
        f.write(texto)   
        f.write("Isto vai ser guardado num ficheiro.")
        for i in range(len(data)):
                dataPunctuation.append(dataLower[i]) #primeiro atribuir o valor
                f.write(dataPunctuation[i]) #depois escrever

    f.close()

if __name__ == "__main__":
    ReadFile()
    PunctuationLowercase()
    SaveFile()

I hope I have helped. Hugs,

    
10.12.2018 / 17:13