如何使用Python将JSON解析为CSV中的两列

时间:2013-06-26 23:53:35

标签: python json parsing csv

出于某种原因,我无法在家用计算机上登录与工作计算机相同的帐户 我能够得到Bo10的代码,但不是异常的,我真的很想理解为什么。

以下是我对abernert代码的更新:

    import csv
    import sys
    import json
    import urllib2

    j = urllib2.urlopen('https://citibikenyc.com/stations/json')
    js = json.load(j)

    citi = js['stationBeanList']

    columns = ('stationName', 'totalDocks', 'availableDocks', 
               'latitude', 'longitude', 'availableBikes')
    stations = (operator.itemgetter(columns)(station) for station in citi)

    with open('output.csv', 'w') as csv_file:
        csv_writer = csv.writer(csv_file)
        csv_file.writerows(stations)

I thought adding this line `csv_writer = csv.writer(csv_file)` would fix the object has no attirbute error, but I am still getting it.  This is the actual error:

Andrews-MacBook:coding Andrew$ python citibike1.py
Traceback (most recent call last):
  File "citibike1.py", line 17, in <module>
    csv_file.writerows(stations)
AttributeError: 'file' object has no attribute 'writerows'

所以现在我已将代码更改为此,输出只是重复列的名称322次。我在第14行更改了它,因为我收到了这个错误:

Traceback (most recent call last):
  File "citibike1.py", line 17, in <module>
    csv_writer.writerows(stations)
  File "citibike1.py", line 13, in <genexpr>
    stations = (operator.itemgetter(columns)(station) for station in citi)
NameError: global name 'operator' is not defined: 

import csv
import sys
import json
import urllib2
import operator

j = urllib2.urlopen('https://citibikenyc.com/stations/json')
js = json.load(j)

citi = js['stationBeanList']

columns = ('stationName', 'totalDocks', 'availableDocks', 
           'latitude', 'longitude', 'availableBikes')
stations = (operator.itemgetter(0,1,2,3,4,5)(columns) for station in citi)

with open('output.csv', 'w') as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerows(stations)

2 个答案:

答案 0 :(得分:1)

问题是您没有使用csv模块,而是使用pickle模块,这就是pickle输出的样子。

修复它:

csvfile = open('output.csv', 'w')
csv.writer(csvfile).writerows(stationList)
csvfile.close()

请注意,您将不遗余力地构建转置表,其中包含6个322列表,而不是322个列表,包含6个列表。所以,你将获得6行322列。如果你想要相反,那就不要这样做:

stationList = []
for f in citi:
    stationList.append((f['stationName'],
                        f['totalDocks'],
                        f['availableDocks'],
                        f['latitude'],
                        f['longitude'],
                        f['availableBikes']))

或者,更简单地说:

stationlist = map(operator.itemgetter('stationName', 'totalDocks', 'availableDocks',
                                      'latitude', 'longitude', 'availableBikes'),
                  citi)

但是,您可能需要考虑一次只编写一行,而不是构建一个庞大的列表。

您可以通过将csv.writerow调用放入for循环的中间来实现。

但您也可以通过使用itertools.imap或生成器表达式而不是map或列表推导来实现。这将使stationlist成为一个可根据需要创建新值的迭代,而不是一次创建所有值。


将所有这些放在一起,这就是我编写程序的方式:

import csv
import sys
import json
import urllib2

j = urllib2.urlopen('https://citibikenyc.com/stations/json')
js = json.load(j)

citi = js['stationBeanList']

columns = ('stationName', 'totalDocks', 'availableDocks', 
           'latitude', 'longitude', 'availableBikes')
stations = (operator.itemgetter(columns)(station) for station in citi)

with open('output.csv', 'w') as csv_file:
    csv.writer(csv_file).writerows(stations)

答案 1 :(得分:0)

正如abarnert所提到的,你实际上并没有使用你导入的csv模块。

此外,您存储列的逻辑实际上可能会被转置。我想你可能想要这样做(编辑以修复元组/列表混淆):

import csv
import json
import urllib2

j = urllib2.urlopen('https://citibikenyc.com/stations/json')
js = json.load(j)

citi = js['stationBeanList']

columns = ["stationName", "totalDocks", "availableDocks", "latitude", "longitude", "availableBikes"]
station_list = [[f[s] for s in columns] for f in citi]

with open("output.csv", 'w') as outfile:
  csv_writer = csv.writer(outfile)
  csv_writer.writerows(station_list)