2

I am currently working on an application which grabs data from subreddits and writes them into an text file. When starting my script I am running in the following error:

  File "my_file.py", line 35, in <module>
    data.writelines('parent_id: '+ str(comment.parent()) + 'body: '+ str(comment.body) + "\n")
  File "C:\Users\supre\anaconda3\envs\learn-python\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f937' in position 0: character maps to <undefined> 

This is my code:

import praw


reddit = praw.Reddit(client_id = 'my_client_id',
                     client_secret = 'my_client_secret',
                     username = 'my_username',
                     password = 'my_password',
                     user_agent = 'my_user_agent')
                    
subreddit = reddit.subreddit('news')

hot_python = subreddit.hot(limit=50)

data = open('data.txt','w')
for submission in hot_python:
    if not submission.stickied:
        print(20*'-')
        print('TITEL: '+ submission.title)
        
        
        submission.comments.replace_more(limit=0)
        for comment in submission.comments.list():
            print(30*'+')
            print('parent_id:', comment.parent())
            print(comment.body)
            if len(comment.replies) > 0:
             for reply in comment.replies:
                    print('REPLY:')
                    print("\t"+reply.body)
                    print(reply.parent())

    
            data.writelines('parent_id: '+ str(comment.parent()) + 'body: '+ str(comment.body) + "\n")
data.close()

Is there maybe something I am doing wrong? If so I would be glad if someone could tell me what I am doing wrong and help me to solve this problem.

Thank's for every suggestion and help in advance:)

1 Answers1

3

Try:

data = open('data.txt','w', encoding='utf-8')
Francisco Wendel
  • 618
  • 2
  • 5
  • 13