当地时间的优秀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;