我如何用python解析json api响应?我目前有这个:
import urllib.request
import json
url = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'
def response(url):
with urllib.request.urlopen(url) as response:
return response.read()
res = response(url)
print(json.loads(res))
Run Code Online (Sandbox Code Playgroud)
我收到此错误:TypeError:JSON对象必须是str,而不是'bytes'
什么是pythonic方式来处理json apis?
我正在阅读一些关于英特尔操作码汇编指令的资料,但我无法理解它遵循操作码字节是什么意思.例如:"cw","cd","/ 2","cp","/ 3".请给我一个提示是什么意思或在哪里可以找到完整的参考?提前致谢!
E8 cw CALL rel16相对于下一条指令调用near,relative,displacement
E8 cd CALL rel32相对于下一条指令调用near,relative,displacement
FF / 2 CALL r/m16调用r/m16中给出的接近绝对间接地址
FF / 2 CALL r/m32调用r/m32中给出的接近绝对间接地址
9A cd CALL ptr16:16调用操作数中给出的far,absolute,address
9A cp CALL ptr16:32调用操作数中给出的far,absolute,address
FF / 3 CALL m16:16调用m16:16中给出的远,绝对间接地址
FF / 3 CALL m16:32调用m16:32中给出的远,绝对间接地址
我刚刚开始尝试学习如何在 discord.py 中编码。但是,我不确定如何检查用户是否在命令后提供了争论。例如,如果用户输入!kick,但没有在其后标记用户,它应该给他们一条错误消息,说“请标记用户”。
所以我的问题是,如何检查用户在使用命令时是否标记了另一个用户,如果没有,给他们一条错误消息。
这是我的代码:
#PyBot by RYAN#2302
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import chalk
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print ("Ready when you are xd")
print ("I am running on " + bot.user.name)
print ("With the ID: " + bot.user.id)
@bot.command(pass_context=True)
async def ping(ctx):
await bot.say(":ping_pong: Ping! :ping_pong:")
print ("!ping")
@bot.command(pass_context=True)
async def info(ctx, user: discord.Member):
await bot.say("The users name is: {}".format(user.name))
await bot.say("The …Run Code Online (Sandbox Code Playgroud) 我无法print在类中执行函数:
#!/usr/bin/python
import sys
class MyClass:
def print(self):
print 'MyClass'
a = MyClass()
a.print()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
File "./start.py", line 9
a.print()
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我不了解python的行为,我有很多相同的错误:
文件“ ./start.py”,第2行,在print2
item.print2()中
RuntimeError:超过最大递归深度
我的代码如下:
#!/usr/bin/python
class Component:
def print2(self):
pass
class Composite(Component):
items = []
name = 'composite'
def __init__(self, name):
self.name = name
def add(self, item):
self.items.append(item)
def print2(self):
if self.items:
for item in self.items:
item.print2()
else:
print self.name
class Leaf(Component):
name = 'leaf'
def __init__(self, name):
self.name = name
def print2(self):
print self.name
category = Composite('category1')
leaf = Composite('leaf1')
category.add(leaf)
leaf = Leaf('leaf2')
category.add(leaf)
category.print2()
Run Code Online (Sandbox Code Playgroud)
当我添加self.items = []构造函数(__init__)时,它可以正常工作。您能解释一下这种行为吗?
函数'hasattr()'不能像我在Python中预期的那样工作
我有以下代码:
#!/usr/bin/python
import re
import os
import sys
results=[{'data': {}, 'name': 'site1'}, {'data': {u'Brazil': '5/1', u'Panama': '2000/1'}, 'name': 'site2'}]
print results[1]
if hasattr(results[1]['data'], u'Brazil'):
print 'has'
else:
print 'hasn\'t'
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它给我输出:hasn't.
我不明白如果它存在,如何检查属性.
我试图删除u之前,Brazil但它不起作用.
怎么解决?
python ×5
api ×1
assembly ×1
class ×1
dictionary ×1
discord ×1
discord.py ×1
hasattr ×1
intel ×1
json ×1
machine-code ×1
opcode ×1
python-2.x ×1
python-3.x ×1
x86 ×1