Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python读取Json数据时停止工作_Python_Json - Fatal编程技术网

Python读取Json数据时停止工作

Python读取Json数据时停止工作,python,json,Python,Json,我使用的是来自 实际上这就是代码 from datapackage import Package package = Package('http://datahub.io/core/population/datapackage.json') # get list of resources: resources = package.descriptor['resources'] resourceList = [resources[x]['name'] for x in range(0, len

我使用的是来自

实际上这就是代码

from datapackage import Package

package = Package('http://datahub.io/core/population/datapackage.json')

# get list of resources:
resources = package.descriptor['resources']
resourceList = [resources[x]['name'] for x in range(0, len(resources))]

data = package.resources[0].read()
print(data)
这是返回的数据-

Country Name,Country Code,Year,Value
Arab World,ARB,1960,92490932
Arab World,ARB,1961,95044497
Arab World,ARB,1962,97682294
Arab World,ARB,1963,100411076
Arab World,ARB,1964,103239902
现在,这条线

数据=包。资源[0]。读取

停止工作。这是错误-非表格数据不支持iter/read方法

然后我将它们加载到SQL中的一个表中

for i in range(len(data)):
    Val1 = data[i][0]
    Val2= data[i][1]
    Val3= data[i][2]
    Val4= data[i][3]
    cursor.execute("insert into Population_Country (CountryName,CountryCode,Year,PopulationNumber) values (?,?,?,?)", Val1, Val2, Val3, Val4)
    cnxn.commit()

不确定之前是怎么回事,但如果你看看里面有什么资源:

['validation_report', 'population_csv', 'population_csv_preview', 'population_json', 'population_zip', 'population']
因此,0报告是带有下一个描述符的“验证报告”:

{'bytes': 598, 'datahub': {'type': 'derived/report'}, 'description': 'Validation report for tabular data', 'dpp:streamedFrom': 'validation_report.json', 'encoding': 'utf-8', 'format': 'json', 'name': 'validation_report', 'path': 'https://pkgstore.datahub.io/core/population/validation_report/data/b6445474af133352488c54ed6c6fd6f2/validation_report.json', 'profile': 'data-resource'}
而且它不是表格,所以不能用read读取。 据我所知,您需要人口和csv资源,因此我将采取如下方式:

package.get_resource("population_csv").read()

你有熊猫吗?是的,我有熊猫。但是如果你指的是这本书中的代码-。我不知道如何获取数据,因为我需要类似于问题中的数据的输出。我使用了请求,发现这个包分两步加载数据。首先是元数据,读取实际数据的路径,其中包括其他数据,然后读取该路径。@cᴏʟᴅsᴘᴇᴇᴅ 那么代码哪里出了问题,如果你有一个工作代码,你能把它作为解决方案发布吗