python处理ini详解_使用.ini选项解析Python JSON

当地时间的优秀Python人

我将处理一堆JSON响应,格式如下:{

"responseHeader":{

"status":1,

"params":{

"indent":"true",

"fq":"recordType:Vinyl",

"wt":"json"

}

},

"response":{

"numFound":2,

"albums":[

{

"name":"Some Crappy Album",

"year":"1997",

"artist":[

"Bill's Polka Jamburri"

],

"producer":[

"Dope records"

],

},

{

"name":"Best of Foreigner",

"year":"2008",

"artist":[

"Foreginer"

],

"producer":[

"Rhino Entertainment"

],

},

]

}

}

以及一个.ini文件,其中包括:

^{pr2}$

我已经有了使用urllib、urllib2、argparse和config parser的代码,这些代码能够读入一堆记录并处理数据。我的问题是,使用.ini文件实现过滤的最佳方式是什么?在这个文件中,我可以根据字段显式地检索相册(包括:艺术家=德沃)或基于字段排除相册(排除:年份=1979)?

下面是我的getOptionsFromConfigFile、loadJSON和getAlbums函数:def getOptionsFromConfigFile( ):

print "==========================================================================="

print "Reading in config (.ini) file params ... "

config = ConfigParser.ConfigParser()

config.read("config.ini")

ExcludeParams = config.get("Filters", "Exclude")

logging.debug(' Exclude params pulled from ini file: ' + JSONPath)

IncludeParams = config.get("Filters", "Include")

logging.debug(' Include params pulled from ini file: ' + JSONPath)

return ExcludeParams, IncludeParams;

def loadJSON( ):

print "Fetch Albums! ---> " + JSONPath

print "==========================================================================="

logging.debug('Loading ' + JSONPath)

response = urllib2.urlopen(JSONPath)

data = response.read()

values = simplejson.loads(data)

logging.debug('Dictionary pulled from ' + JSONPath)

return values;

def getAlbums( values, outputPath):

logging.debug('Getting Albums ...')

for Album in values['response']['albums']:

albumName = album['name']

storeAlbum(outputPath)

print "==========================================================================="

return;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值