Saturday, 8 May 2021

Adding comma to lines of a text file

#adding comma at the end of each line in the text file.

#codes:


with open("data.txt", "r") as y, open("dataout5.txt", "w") as butt:

 

 

#here, data.txt is the original file and dataout5.txt is the file we are creating and where we will have the commas added after each line.

#we called the old file as y and new file as butt. 

 
    for line in y:
        butt.write(line.strip() + ',\n') 

#this works as well like the rstrip() one

 

 

# how to read the new file: 

with open("dataout5.txt", "r") as butt:
    print(butt.read())

 

#this is how it will look after executing: 

 

 


 

 




No comments:

Post a Comment