将JSON数据导入CSV格式

时间:2014-11-01 21:40:12

标签: python json parsing csv import

我的最终目标是将API调用中的JSON数据解析为CSV文件。我正在使用Yelp API http://www.yelp.com/developers/documentation/v2/examples的示例代码。我得到的最好的是将数据写入CSV文件,但它不是很漂亮。我相信我首先缺少将数据放入Python字典,但我研究并尝试了一些让我无处可去的方法。

try:
    CSVfile = open('C:\Users\Petr Fedorov\Desktop\YelpAPI.csv', "wb")
    response = json.dump(conn.read(),CSVfile,sort_keys=True)
    #response = json.loads(conn.read()) #this is the original code under try:

finally:
    conn.close()
    CSVfile.close()

return response

我查看了以下帖子,但未能采用他们的方法。

Parsing values from a JSON file using Python? How do I write JSON data to a file in Python? Creating a JSON response using Django and Python

非常感谢您提供代码或指导我正确方向的时间和精力。

以下是带有原始代码的示例输出... response = json.loads(conn.read())

Result for business "affordable-interior-design-new-york" found:
{ u'categories': [[u'Interior Design', u'interiordesign']],
  u'display_phone': u'+1-917-686-1319',
  u'id': u'affordable-interior-design-new-york',
  u'image_url': u'http://s3-media1.fl.yelpcdn.com/bphoto/taBV7OM7mNyS_pQD7ifSVw/ms.jpg',
  u'is_claimed': True,
  u'is_closed': False,
  u'location': { u'address': [],
                 u'city': u'New York',
                 u'coordinate': { u'latitude': 40.7376543584561,
                                  u'longitude': -74.0053272269531},
                 u'country_code': u'US',
                 u'display_address': [u'West Village', u'New York, NY 10014'],
                 u'geo_accuracy': 9.5,
                 u'neighborhoods': [u'West Village'],
                 u'postal_code': u'10014',
                 u'state_code': u'NY'},
  u'mobile_url': u'http://m.yelp.com/biz/affordable-interior-design-new-york',
  u'name': u'Affordable Interior Design',
  u'phone': u'9176861319',
  u'rating': 5.0,
  u'rating_img_url': u'http://s3-media1.fl.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png',
  u'rating_img_url_large': u'http://s3-media3.fl.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png',
  u'rating_img_url_small': u'http://s3-media1.fl.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png',
  u'review_count': 10,
  u'reviews': [ { u'excerpt': u'Classes are structured in a very practical manner. It sets up a basic layout and gives you a foundation about what to take care of step-by-step.\nAnd yes,...',
                  u'id': u'Vu5-xvjUII0wCV3Q-1vz8w',
                  u'rating': 4,
                  u'rating_image_large_url': u'http://s3-media2.fl.yelpcdn.com/assets/2/www/img/ccf2b76faa2c/ico/stars/v1/stars_large_4.png',
                  u'rating_image_small_url': u'http://s3-media4.fl.yelpcdn.com/assets/2/www/img/f62a5be2f902/ico/stars/v1/stars_small_4.png',
                  u'rating_image_url': u'http://s3-media4.fl.yelpcdn.com/assets/2/www/img/c2f3dd9799a5/ico/stars/v1/stars_4.png',
                  u'time_created': 1324256900,
                  u'user': { u'id': u'r9ecgI5mnHgPo4W0fPRqPA',
                             u'image_url': u'http://s3-media4.fl.yelpcdn.com/photo/kcsQCRHtTmZcwPtE7_aeAQ/ms.jpg',
                             u'name': u'Wendy G.'}}],
  u'snippet_image_url': u'http://s3-media4.fl.yelpcdn.com/assets/2/www/img/cc4afe21892e/default_avatars/user_medium_square.png',
  u'snippet_text': u'So much more than I could have hoped for. Betsy is fun, funny, and REALLY good at Interior Design.\n\nThe 3 hour plan was perfect.',
  u'url': u'http://www.yelp.com/biz/affordable-interior-design-new-york'}

1 个答案:

答案 0 :(得分:1)

实际上,你的回复已经是python词典了。您可以做的一件事是使用get put将数据放在pandas DataFrame对象中。然后从那里输出到csv。

您可以直接从json字符串

执行此操作
import pandas as pd
df = pd.read_json(conn.read())

或者你可以从python词典中做到这一点。

df = pd.from_dict(response)

然后您可以将其输出到csv

df.to_csv('file.csv')

一个问题是你的反应超过1级。换句话说,有嵌套的词典。你需要有这种格式的东西

r = {'a': [list], 'b': [list] , 'c': value, 'd':value}

换句话说,这些值不能是字典。这意味着您仍然需要操纵字典数据,以便首先将其输入正确的形式,就像选择数据的某些部分一样。