Python将列表中的各个元素拆分为较小的字符串

时间:2016-07-03 03:24:58

标签: python

我正在服用json并将其转换为csv。

我可能会解决这个问题,但我很新。

我正在打电话。

一次api通话会返回25个结果。

如果我只是获取我想要的数据,我将留下25个元素列表。一个工作结果是一个元素。

但我需要能够将个人工作结果分解为其组成部分。职称,日期,地点,公司等。

代码并不重要,但无论如何它仍然存在:

import json
import requests

api_url = 'http://api.indeed.com/ads/apisearch?publisher=################&v=2&limit=100000&format=json'
number= 0
SearchTerm = 'Birst'

for number in range(0, 75, 25): 
    url = api_url + '&q=' + SearchTerm + '&latlong=1' + '&start=' + str(number)
    response = requests.get(url)
    grabforclean = json.loads(response.content)
    clean_json = (grabforclean['results'])

    print 'Grabbed'
    print len(clean_json)
    print clean_json[1]

这是输出:

Grabbed
25
{u'formattedRelativeTime': u'30+ days ago', u'city': u'San Francisco', u'date': u'Fri, 27 May 2016 03:06:26 GMT', u'latitude': 37.774727, u'url': u'http://www.indeed.com/viewjob?jk=68a1f3ca57c87b65&qd=S0eImTYbjbDl0FpDWMH7yxn390IaFpVEIOijV-ObcSMguXFXi22BmSZ0-mFRI3DxL4I4cyMEC3X3Sq1uL-fv4am1Aj3izkOw87NHJgxznYA&indpubnum=8710117352111766&atk=1amnato8gb8tqe41', u'jobtitle': u'Senior Customer Success Manager', u'company': u'Birst', u'formattedLocationFull': u'San Francisco, CA', u'longitude': -122.41758, u'onmousedown': u"indeed_clk(this, '4355');", u'snippet': u'Develop a trusted advisor relationship with customer sponsors such that all <b>Birst</b> activities are closely aligned with the customer\u2019s business goals and strategy...', u'source': u'Birst', u'state': u'CA', u'sponsored': False, u'country': u'US', u'formattedLocation': u'San Francisco, CA', u'jobkey': u'68a1f3ca57c87b65', u'expired': False, u'indeedApply': False}

感谢。

我终于解决了它,结果是通过使用dictwriter我没有打破那些字典,只需要使用unciodecsv进行编码并在循环调用之前打开csv:

import unicode csv as csv
...
with open('testingdictastaa.csv' , 'w' ) as csvfile:
    fieldnames = ['city','company','country','date','expired','formattedLocation','formattedLocationFull','formattedRelativeTime','indeedApply','jobkey','jobtitle','latitude','longitude','onmousedown','snippet','source','sponsored','state','url']
    writer = csv.DictWriter(csvfile, fieldnames = fieldnames, lineterminator = '\n')
    writer.writeheader()
    for number in range(0, 75, 25): 
        url = api_url + '&q=' + SearchTerm + '&latlong=1' + '&start=' + str(number)
        response = requests.get(url)
        grabforclean = json.loads(response.content)
        clean_json = (grabforclean['results'])
        print 'Grabbed'
        for job in clean_json:
            writer.writerow(job) 

1 个答案:

答案 0 :(得分:1)

你所拥有的确实是一本字典,而不是一个列表,因为它由键:值对组成。使用Python循环遍历字典:

for k, v in clean_json.items():
    ... # do something with your data

clean_json.items()返回字典中的键:值对列表,通过循环变量kv可以相应地访问它们。

出于您的目的,只需使用外循环:

for job in clean_json:
    for k, v in job.items():
        ... # do something