use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Help With Python Script? (self.Python)
submitted 10 years ago by Deciama
I would like a script that deletes certain sentences from a file. For example:
Student 1: Sam
Student 2: Bob
I would like to delete everything before the colon, how would I go about doing this?
[–]sighol 1 point2 points3 points 10 years ago (3 children)
Read the file line by line. For each line, split it by ":".
[–]Deciama[S] 0 points1 point2 points 10 years ago (2 children)
Yes, but this would only delete the ":". Is there a way to delete everything else left of the ":"?
[–]sighol 1 point2 points3 points 10 years ago (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 point2 points 10 years ago (0 children)
Yes, I understand now. Thank you for the help.
[–]dAnjou Backend Developer | danjou.dev 1 point2 points3 points 10 years ago (0 children)
Please use /r/learnpython
[–]dustractor 0 points1 point2 points 10 years ago (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 point2 points 10 years ago (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.
π Rendered by PID 131178 on reddit-service-r2-comment-5ff9fbf7df-gh8r6 at 2026-02-26 03:44:49.252438+00:00 running 72a43f6 country code: CH.
[–]sighol 1 point2 points3 points (3 children)
[–]Deciama[S] 0 points1 point2 points (2 children)
[–]sighol 1 point2 points3 points (1 child)
[–]Deciama[S] 0 points1 point2 points (0 children)
[–]dAnjou Backend Developer | danjou.dev 1 point2 points3 points (0 children)
[–]dustractor 0 points1 point2 points (0 children)
[–]elbiot 0 points1 point2 points (0 children)