How to parse JSON to two columns in a CSV with Python
Question: For some reason I can't log into the same account on my home computer as my
work computer.  
I was able to get Bo10's code to work, but not abernert's and I would really
like to understand why.

Here is my updates to abernert's code:

    
    
        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'
    

* * *

So now I have the changed the code to this and the output is just repeating
the names of the columns 322 times. I changed it on line 14 because i was
getting this error:

    
    
    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)
    


Answer: The problem is that you're not using the `csv` module, you're using the
`pickle` module, and this is what `pickle` output looks like.

To fix it:

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

* * *

Note that you're going out of your way to build a transposed table, with 6
lists of 322 lists, not 322 lists of 6 lists. So, you're going to get 6 rows
of 322 columns each. If you want the opposite, just don't do that:

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

Or, more briefly:

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

* * *

However, instead of building up a huge list, you may want to consider writing
the rows one at a time.

You can do that by putting `csv.writerow` calls into the middle of the for
loop.

But you can also do that just by using `itertools.imap` or a generator
expression instead of `map` or a list comprehension. That will make
`stationlist` into an iterable that creates new values as needed, instead of
creating them all at once.

* * *

Putting that all together, here's how I'd write your program:

    
    
    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)
    

