Python处理json数据

阅读: 评论:0

Python处理json数据

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 = w()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 = adlines()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 contextdef getbeforeDay(n=0):now_time = w()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 ists(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 srcdataprint srcdatajsondata = json.loads(srcdata)#国家代码code = jsondata['code']#国家中文名称cn = jsondata['cn']#国家英文名称en = jsondata['en']#面积area = jsondata['area'].replace(',','')#人口population = jsondata['population'].replace(',','')#iso2iso2 = jsondata['iso2']#iso3iso3 = jsondata['iso3']#print code,cn,en,area,population,iso2,iso3context = getFormateContext(code,cn,en,area,population,iso2,iso3)print contextWrite("country.csv", context, model='a')

4.最终处理数据结果

   CSV格式: 

   Excel格式:

 

转载于:.html

本文发布于:2024-01-31 00:32:31,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170663235223956.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   Python   json
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23