json - 编码适用于 1 而不是 Twitter 中使用 python 的其他列表
问题描述
我正在尝试使用 twitter 模块和 python 从 twitter 开始作弊数据。这是我的代码
import twitter
import win_unicode_console
win_unicode_console.enable()
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxx'
OAUTH_TOKEN = 'xxxxxxxxxxxxxxxxx'
OAUTH_TOKEN_SECRET = 'xxxxxxxxxxxx'
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
print(twitter_api)
WORLD_WOE_ID = 1
US_WOE_ID = 23424977
world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)
print(us_trends)
print(world_trends)
我收到编码错误。所以我用
print((us_trends).encode('utf-8'))
这导致
AttributeError: 'TwitterListResponse' object has no attribute 'encode'
所以我决定使用 win_unicode_console 模块
但令人困惑的是 us_trends 正在返回值。
[{'trends': [{'name': 'El Chapo', 'url': 'http://twitter.com/search?q=%22El+Chapo%22', 'promoted_content': None, 'query': '%22El+Chapo%22', 'tweet_volume': 103536}, {'name': 'Antonio Brown', 'url': 'http://twitter.com/search?q=%22Antonio+Brown%22', 'promoted_
但声明
print(world_trends)
给出以下错误
File "C:\Users\nawendu\Desktop\TWIT.PY", line 25, in <module>
print(world_trends)
File
line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 24-
29: character maps to <undefined>
编码如何在我们的趋势中而不是在世界趋势中起作用?
解决方案
encode
是一个字符串的方法。
你有一个 json 对象,它没有这个方法。
当你print
是一个对象时,它需要将对象转换为你的输出编码的字符串表示形式(这里可能是 windows 编码)。如果其中有字符(例如表情符号)不在输出编码中,则会出现错误。
编码是一个困难的话题(也是 Python 中的一个痛点),但如果你想打印输出,你需要了解它们。
推荐阅读
- swagger - 如何在 OpenAPI 3.0 中定义标头参数?
- sql - SQL 员工在同一部门搜索其他人
- mysql - 在 Django 中将 SQLite3 数据库迁移到 MySQL
- java - 如何从 SOAPui 发送到 Wiremock 服务器的请求中捕获 XML 消息以与 XSD 文件进行比较
- python - Pandas groupby value_count 按频率过滤
- c# - 泛型类型约束语法与继承语法冲突
- c# - 如何从 C# 中的 blob URL 获取音频文件?
- javascript - 清除输入字段而不重新加载页面
- c++ - 使用 glTexCoordPointer() 的问题
- php - 在 Laravel 中将 SQL 更改为查询生成器