Python处理json数据--世界国家维度数据
1.准备国家的json数据
将准备好的json数据放在指定的目录下,此处可以重这里下载
2.测试编写python脚本处理json提取字段值
#coding:utf8 import time, re, os, sys, time,urllib2,shutil,string import json,datetime #设置utf-8编码格式 reload(sys) sys.setdefaultencoding( "utf-8" ) #获取当前日期的前n天 def getbeforeDay(n=0): now_time = datetime.datetime.now() beforeday = now_time - datetime.timedelta(n) return beforeday.strftime("%Y%m%d") scriptDir = os.getcwd() if len(sys.argv) > 1 : job_date_id = sys.argv[1] else : job_date_id = getbeforeDay(0) print "当前脚本路径:%s,当前参数日期:%s" % (scriptDir,job_date_id) srcdata='{"area":"390,580","code":"263","en":"Zimbabwe","cn":"津巴布韦","iso2":"ZW","iso3":"ZWE","population":"11,651,858"}' jsondata = json.loads(srcdata) print type(jsondata) print "######遍历key,values######" for key in jsondata.keys(): print key,":",jsondata[key] print "####或者指定key,返回结果####" print jsondata['code'],jsondata['cn'],jsondata['en'],jsondata['area'],jsondata['population']
3.读取文本循环遍历提取字段值
#coding:utf8 import requests, json, time, re, os, sys, time,urllib2,shutil,string import json,datetime #设置utf-8编码格式 reload(sys) sys.setdefaultencoding( "utf-8" ) #读取文件内容 def getLines(filename): file_object = open(filename,'rb') lines = file_object.readlines() return lines #返回规范字符串 def getFormateContext(*name): format = ',' context = name[0] for i in name[1:]: context = context + format + str(i) context = str(context).replace('(', '(').replace(')', ')').replace(',', ',').replace(':', ':') return context def getbeforeDay(n=0): now_time = datetime.datetime.now() beforeday = now_time - datetime.timedelta(n) return beforeday.strftime("%Y%m%d") #写文件 def Write(filename,context,model='a'): #去除首位空格 filename = filename.strip() #读取目录名称 path = os.path.dirname(filename) #如果目录不存在则创建目录 if not os.path.exists(path): pass #读取文件名称 name = os.path.basename(filename) fp = open(filename,model) fp.write(context+'\n') fp.close() scriptDir = os.getcwd() if len(sys.argv) > 1 : job_date_id = sys.argv[1] else : job_date_id = getbeforeDay(10) print "当前脚本路径:%s,当前参数日期:%s" % (scriptDir,job_date_id) filename="%s\/jsondata\/country.json" % (scriptDir) for line in getLines(filename): line = line.strip() line = line[1:] line = line[:-2] for value in line.split("},"): srcdata = value+"}" #print srcdata print srcdata jsondata = json.loads(srcdata) #国家代码 code = jsondata['code'] #国家中文名称 cn = jsondata['cn'] #国家英文名称 en = jsondata['en'] #面积 area = jsondata['area'].replace(',','') #人口 population = jsondata['population'].replace(',','') #iso2 iso2 = jsondata['iso2'] #iso3 iso3 = jsondata['iso3'] #print code,cn,en,area,population,iso2,iso3 context = getFormateContext(code,cn,en,area,population,iso2,iso3) print context Write("country.csv", context, model='a')
4.最终处理数据结果
CSV格式:
Excel格式: