I'm using Reddit's PRAW to collect submissions. I want to be able to slice the text of the submission and save it into a variable to compare against a hardcoded number to remove every submission post that has a number greater than it in its title.
import praw
reddit = praw.Reddit(client_id = 'REDACTED',
client_secret = 'REDACTED',
username = 'REDACTED', password = 'REDACTED',
user_agent = 'REDACTED')
subreddit = reddit.subreddit('NumbersReddit')
new_numpost = subreddit.new(limit=10)
for submission in new_numpost :
if not submission.stickied:
print(50*'-')
print('User: {} '.format(submission.author))
print('Title: {}'.format(submission.title))
print('URL: {}'.format(submission.url))
Using the code above as a starting point: After removing the stickied submissions, I want to get the first two characters of the title of the post (which are and always will be 2 numbers, ie 24) and compare that against a hardcoded number, such as 18, so it then removes all posts that start with numbers greater than 18.
The following should be the output:
User: Example1
Title: 18 Years and I haven't realised this before
URL: https://www.reddit.com/r/NumbersReddit/comments/example1
User: Example2
Title: 18 times I tried this but it was worth it
URL: https://www.reddit.com/r/NumbersReddit/comments/example2
User: Example3
Title: 18 people said dieting was enough for them
URL: https://www.reddit.com/r/NumbersReddit/comments/example3
Changed this:
for submission in new_numpost :
if not submission.stickied:
print(50*'-')
print('User: {} '.format(submission.author))
print('Title: {}'.format(submission.title))
print('URL: {}'.format(submission.url))
To this:
for submission in new_numpost :
if not submission.stickied:
title_number = int(submission.title[:2])
if title_number < 25:
print(50*'-')
print('User: {} '.format(submission.author))
print('Title: {}'.format(submission.title))
print('URL: {}'.format(submission.url))
To get the first n number of characters of any string do the following:
(Assuming your title is "18: Hello!")
my_title = '18: Hello!'
print(my_title[:2])
Results in:
18
So for you you'd do the following on your title line:
print('Title: {}'.format(submission.title[:2]))
Assuming it's a string in there.
Once you have that, you can convert it to an integer like so (assuming we haven't sliced it yet):
title = int(title[:2])
Then you can do whatever comparison you want with the int.
Updated to further clarify answer:
It seems your full title comes from submission.title
, so what you can do is create two variables based on that:
One variable named title_number
and another named title
where title_number = int(submission.title[:2])
and title = submission.title
and all you do is compare the title_number
and if it is greater than whatever number you get print the title
in your for loop otherwise ignore it.
For example (assuming submission.title is '18 Years and I haven't realised this before')
check_int = 17
for submission in new_numpost:
title = submission.title
title_number = int(submission.title[:2])
if title_number > check_int:
print(title)
Should result in:
18 Years and I haven't realised this before
Being printed out