2016-07-03 6 views
0

私はjsonをとり、csvに変換しています。Pythonリストの個々の要素を小さな文字列に分割します

私はこの問題への私のアプローチでは邪魔になるかもしれませんが、私はかなり新しいです。

私はapi呼び出しを行っています。

1回のAPIコールで25件の結果が返されます。

私が欲しいデータを取得するだけであれば、25個の要素リストが残っています。 1つの仕事の結果は1つの要素です。

しかし、私は個々の仕事の結果をその構成部分に分割することができる必要があります。など

Jobtitle、日付、場所、会社は、コードは本当に重要ではありませんが、ここでは、とにかくです:

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} 

ありがとう:ここ

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] 

が出力されます。

私は最終的にちょうどエンコードにunciodecsv使用し、呼び出しをループする前に、CSVを開くために持っていた、dictwriterを使用して、私はそれらの辞書を分割する必要はありませんでしたことが判明し、それを解決:

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) 
+0

をあなたの質問は何ですか? –

+0

リスト要素を小さな部分に分割するにはどうすればよいですか? –

答えて

0

あなたが持っているものは実際には辞書であり、リストではありません。それはキー:値のペアで構成されているからです。使用Pythonの辞書を反復処理するforループ:ループ変数kvを通じて相応にアクセス可能な辞書からの値のペア、:

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

clean_json.items()は、キーのリストを返します。あなたの目的のために


、ちょうど外側のループを使用します。

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

ありがとう、私はforループのために辞書のリストがあると思います。私がclean_json [1] .values()を印刷すると、ブラケットに3つの別々の結果が表示されます –

+0

投稿を編集しました。また、辞書のvalues()を呼び出すと、すべての値のリストが返されます。何が問題なのですか? –

+0

私は助けてくれてありがとう、私はちょうどデバッグを学ぶ必要があると思う。問題は今ではエンコーディングですが、残念ながらすべてのフィールドが文字列であるわけではないので、floatはutf-8に変換できません。私は今、いくつかの睡眠を取らなければならない、私は明日それに戻ってきます。 –

関連する問題