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()