Home > Software design >  'set' object has no attribute 'get' CSV (in praw)
'set' object has no attribute 'get' CSV (in praw)

Time:02-12

This code is supposed to get some information about a post like title, upvotes etc. And then write it in a CSV file. But when is run it I get this error:

Traceback (most recent call last):
  File "main.py", line 71, in <module>
    writer.writerow(data)
  File "/usr/lib/python3.8/csv.py", line 154, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
  File "/usr/lib/python3.8/csv.py", line 151, in <genexpr>
    return (rowdict.get(key, self.restval) for key in self.fieldnames)
AttributeError: 'set' object has no attribute 'get'

And this is the code:

    import praw
    import random
    import time
    import csv
    #from keep_Aliv import keep_alive
    
    username = "XXXXXX"
    
    #keep_alive()
    
    print('AAAAAAA')
    
    reddit = praw.Reddit(client_id="XXXXXX",
                         client_secret="XXXXXX",
                         user_agent="bot",
                         username=username,
                         password='XXXXXX')
    
    
    subreddit = reddit.subreddit('all')
    
   with open('pdata.csv', 'a') as f:
    headers = [
        'ID', 'Date_utc', 'Upvotes', 'Number of Comments', 'Subthread name'
    ]
    writer = csv.DictWriter(f,
                            fieldnames=headers,
                            extrasaction='ignore',
                            dialect='excel')
    writer.writeheader()
    for post in subreddit.stream.submissions():
        #print(post.title)
        
        data = {
                "ID": post,
                "Date_utc": post.created_utc,
                "Upvotes": post.ups,
                "Number of comments": post.num_comments,
                "Subthread name": post.title,
                }
        writer.writerow(data)
        print(data)

ok, so how am I supposed to fix this? pls tell if some other information is required.

Thanks :)

CodePudding user response:

You are using a dict writer, which accepts dicts. You are passing a string to it.

You should construct a dict, and write that:

data = {
    "ID": post,
    "Date_utc": post.created_utc,
    "Upvotes": post.ups,
    "Number of comments": post.num_comments,
    "Subthread name": post.title,
}
writer.writerow(data)

For more information how to use the dictwriter you can go here: https://www.programiz.com/python-programming/writing-csv-files

CodePudding user response:

data does not appear to be in the correct format:

Try the following:

data = {
    'ID' : post, 
    'Date_utc' : post.created_utc, 
    'Upvotes' : post.ups,
    'Number of comments' : post.num_comments,
    'Subthread name' : post.title
}

Note: when using a dictwriter, you should also open you file with the newline parameter as follows:

with open('pdata.csv', 'a', newline='') as f:

This avoids extra newlines in the output.

  • Related