Store the contents of a file in another - python file

Asked

Viewed 244 times

0

I’m trying to read information from a file and save it in a new file, after passing all information to lowercase and removing punctuation characters. For this I created a Readfile() function that reads the file in question and saves it in a global variable, date. After reading the file and storing the information in the date variable you will move to lower case and remove the special characters through the Punctuactionlowercase() function. And finally, I wanted to save this new information in a new file, however I’m not getting it. 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()

1 answer

0

Hello! I believe that you need to use global variables in another way and another point would be with respect to writing in the file (each with its own 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’ve helped.
Hugs,

  • Thank you very much, it helped a lot!!

  • If the answer was helpful, I ask you to vote for it to score and so help other people in the community. Hugs,

Browser other questions tagged

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