Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm a beginner in python and I have a txt file looks like that:

a b c
d e f
g h I

and I want to delete the last column and save the output in a new file to be like that:

a b
d e
g h

how can I do this using python?

What I have tried:

I have watched videos but didn't help
Updated 23-Feb-22 20:52pm

1 solution

Text files don't have columns: the only way to remove anything is to create a new file, then read the old file copying the data you do want and not copying the data you don't.
So use a loop, read each line from the file as a string, then modify the string to remove the data you don't want. Write the changed line to the new file, and repeat.
After the loop, either delete or rename the original file, then rename the new file to the original file name.
Share this answer
Maciej Los 24-Feb-22 13:41pm    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900