作为API的介绍,我试图找出如何使用Rotten Tomatoes API在python中访问数据.这也是我第一次与json打交道.
我正在使用Python 3.4并确认已经安装了json和urllib3.
这是我的代码:
import urllib3
import json
url = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=16&country=us&apikey=API-KEY';
http = urllib3.PoolManager()
request = http.request('GET', url)
print (json.load(request));
request.release_conn()
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Traceback (most recent call last):
File "C:\Users\admnasst1\Documents\Personal\Python\RotTomTest.py", line 16, in <module>
print (str(json.load(request)));
File "C:\Python34\lib\json\__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Python34\lib\json\__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
Run Code Online (Sandbox Code Playgroud)
由于我正在尝试这么多新东西(API,urllib3,json),我不确定发生了什么.我已经尝试过做上面代码的其他一些版本,并且我一直得到同样的错误,所以我想我必须遗漏一些基本的东西......你们有没有发现它?
我正在解决GeeksForGeeks的以下练习问题:
编写一个函数 Add() 返回两个整数之和。该函数不应使用任何算术运算符(+、++、–、-、.. 等)。
C#中给定的解决方案是:
public static int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
// carry now contains common set bits of x and y
int carry = x & y;
// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;
// Carry is shifted by one so that adding it to x …Run Code Online (Sandbox Code Playgroud) 我想使用一个字符串数组来创建几个带循环的变量.
string[] testArray = new string[5] {"Test1", "Test2", "Test3", "Test4", "Test5"};
for (int index = 0; index < testArray.length; index++)
{
string nameArray[index];
}
Run Code Online (Sandbox Code Playgroud)
这给出了两个错误:
我想我明白为什么我尝试的方式不起作用......还有另一种方法可以做到这一点吗?