JSON练习

3.1 导入population.json数据,并格式化输出中国China的人口数据。

下载:population.json

CSV联系

3.2 使用with导入population.csv数据,并输出中国China的人口数据。

下载:population.csv

答案

3.1

import json
fileURL = "population.json"
r = open(fileURL,"r")
popData = json.load(r)
for country in popData:
    if country["Country"] == "China":
        country = json.dumps(country,indent=1)
        print(country)

3.2

import csv
fileURL = "population.csv"
with open(fileURL) as f:
    f_csv = csv.reader(f)
    for row in f_csv:
        if row[0] == "China":
            print(row)

发表评论

邮箱地址不会被公开。 必填项已用*标注