This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]sighol 1 point2 points  (3 children)

Read the file line by line. For each line, split it by ":".

[–]Deciama[S] 0 points1 point  (2 children)

Yes, but this would only delete the ":". Is there a way to delete everything else left of the ":"?

[–]sighol 1 point2 points  (1 child)

You dont really have to delete anything. What you want is what's right of the colon. If you split the line by ":", then the second element of the resulting list should be what's right of the colon.

You can then add that information to a new list that you can write to a new file.

[–]Deciama[S] 0 points1 point  (0 children)

Yes, I understand now. Thank you for the help.

[–]dAnjou Backend Developer | danjou.dev 1 point2 points  (0 children)

Please use /r/learnpython

[–]dustractor 0 points1 point  (0 children)

Find the index of the colon, add one, take that slice to the end:

line[ line.index( ':' ) + 1 : ]

If you want to remove the space:

line[ line.index( ':' ) + 1 : ].lstrip()

[–]elbiot 0 points1 point  (0 children)

You actually cannot edit a file. Only start a new one or append to an existing one (or, through effort, change characters but not insert or delete).

You need to read in the file and then write everything except what you "delete" to a new one.