Search code examples
pythonpraw

Wrapper that guarantees number of lines written to .txt file


Edit:

[ Below is my current code. I am using PRAW to scrape data from reddit. The issue I am having specifically is that some users will have less than 5 submissions to pull from, so what I am needing to do is write a filler line to my txt file in those cases so it does not mess up the pattern for my nth line sorting into lists.

for reddituser in us_list:
    person = reddit.redditor(reddituser)
        x.write(str(person.name))
        x.write('\n')
        x.write(str((person.link_karma) + (person.comment_karma)))
        x.write('\n')
        for submission in person.submissions.new(limit=5):
            x.write(str(submission.title))
            x.write('\n')
            x.write(str(submission.upvote_ratio))
            x.write('\n')

        x.write('\n')

]

Received great help explaining how to enforce the length of a list by appending the end values when applicable. Can be seen here https://stackoverflow.com/a/60482191/12990947

I am scraping data into txt files (values separated by new line) and then turning what I have in the txt files into lists. I am doing this by using readlines and [::5, [1::5] etc.)

Basically I need to take the answer that I received in the link I gave, and apply it to work for the way I am doing this, with new lines.

How can I, within the for loop, write to my txt file the scraped data adding filler line(s) when the available data for a parameter < my defined limit?


Solution

  • You can just count how many records you wrote:

    count = 0
    for submission in person.submissions.new(limit=5):
        x.write(str(submission.title))
        x.write('\n')
        x.write(str(submission.upvote_ratio))
        x.write('\n')
        count += 1
    while count < 5:
        # print empty lines
        count += 1