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()
Thank you very much, it helped a lot!!
– Alice
If the answer was helpful, I ask you to vote for it to score and so help other people in the community. Hugs,
– rammonzito