UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f937' in position 0: character maps to <undefined>
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f937' in position 0: character maps to <undefined>
我目前正在开发一个从 subreddits 中获取数据并将其写入文本文件的应用程序。启动我的脚本时,我 运行 出现以下错误:
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>
这是我的代码:
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()
是不是我做错了什么?如果是这样,如果有人能告诉我我做错了什么并帮助我解决这个问题,我会很高兴。
提前感谢您的每一个建议和帮助:)
尝试:
data = open('data.txt','w', encoding='utf-8')
我目前正在开发一个从 subreddits 中获取数据并将其写入文本文件的应用程序。启动我的脚本时,我 运行 出现以下错误:
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>
这是我的代码:
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()
是不是我做错了什么?如果是这样,如果有人能告诉我我做错了什么并帮助我解决这个问题,我会很高兴。
提前感谢您的每一个建议和帮助:)
尝试:
data = open('data.txt','w', encoding='utf-8')