python - 将 JSON 数据导入为 CSV 格式

标签 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? 奥 git _a How do I write JSON data to a file in 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'}

最佳答案

实际上,你的响应已经是一个Python字典了。您可以做的一件事是使用 get 将数据放入 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}

换句话说,值不能是字典。这意味着您仍然需要首先操作字典数据以将其转换为正确的形式,例如选择数据的某些部分。

关于python - 将 JSON 数据导入为 CSV 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26693544/

相关文章:

python - 使用 Python 和正则表达式查找字符串中的所有中文文本

python - Python 2 中的字母在 Python 3 中的等价性是什么?

android - 在改造android中解析嵌套的json对象

ruby-on-rails - Rails 4 - 简单地在另一个 JSON 中包含多个 JSON

html - 使用 hxselect 在 HTML 中按 ID 标签搜索

python - python 列表中的 Writerows - 如何删除括号和撇号

python - 有人知道 yarowsky 算法的实现吗?

java - 如何防止 eclipse Yasson 输出空字符串和空集合

parsing - 在应用解析中苦苦挣扎

java - jackson 解析复杂对象