writing json-ish list to csv, line by line, in python for bitcoin addresses
<p>I'm querying the <a href="https://api.onename.com/#lookup_users" rel="nofollow">onename api</a> in an effort to get the bitcoin addresses of all the users. </p>

<p>At the moment I'm getting all the user information as a json-esque list, and then piping the output to a file, it looks like this: </p>

<pre><code>[{'0': {'owner_address': '1Q2Tv6f9vXbdoxRmGwNrHbjrrK4Hv6jCsz', 'zone_file': '{"avatar": {"url": "https://s3.amazonaws.com/kd4/111"}, "bitcoin": {"address": "1NmLvYVEZqPGeQNcgFS3DdghpoqaH4r5Xh"}, "cover": {"url": "https://s3.amazonaws.com/dx3/111"}, "facebook": {"proof": {"url": "https://facebook.com/jasondrake1978/posts/10152769170542776"}, "username": "jasondrake1978"}, "graph": {"url": "https://s3.amazonaws.com/grph/111"}, "location": {"formatted": "Mechanicsville, Va"}, "name": {"formatted": "Jason Drake"}, "twitter": {"username": "000001"}, "v": "0.2", "website": "http://1642.com"}', 'verifications': [{'proof_url': 'https://facebook.com/jasondrake1978/posts/10152769170542776', 'service': 'facebook', 'valid': False, 'identifier': 'jasondrake1978'}], 'profile': {'website': 'http://1642.com', 'cover': {'url': 'https://s3.amazonaws.com/dx3/111'}, 'facebook': {'proof': {'url': 'https://facebook.com/jasondrake1978/posts/10152769170542776'}, 'username': 'jasondrake1978'}, 'twitter': {'username': '000001'}, 'bitcoin': {'address': '1NmLvYVEZqPGeQNcgFS3DdghpoqaH4r5Xh'}, 'name': {'formatted': 'Jason Drake'}, 'graph': {'url': 'https://s3.amazonaws.com/grph/111'}, 'location': {'formatted': 'Mechanicsville, Va'}, 'avatar': {'url': 'https://s3.amazonaws.com/kd4/111'}, 'v': '0.2'}}}]
</code></pre>

<p>what I'm really interested in is the field <code>{"address": "1NmLvYVEZqPGeQNcgFS3DdghpoqaH4r5Xh"}</code>, the rest of the stuff I don't need, I just want the addresses of every user. </p>

<p>Is there a way that I can just write only the addresses to a file using python? </p>

<p>I'm trying to write it as something like: </p>

<pre><code>1NmLvYVEZqPGeQNcgFS3DdghpoqaH4r5Xh,
1GA9RVZHuEE8zm4ooMTiqLicfnvymhzRVm,
1BJdMS9E5TUXxJcAvBriwvDoXmVeJfKiFV,
1NmLvYVEZqPGeQNcgFS3DdghpoqaH4r5Xh,
...
</code></pre>

<p>and so on. </p>

<p>I've tried a number of different ways using <code>dump</code>, <code>dumps</code>, etc. but I haven't yet been able to pin it down. </p>

<p>My code looks like this: </p>

<pre><code>import os
import json
import requests
#import py2neo
import csv


# set up authentication parameters
#py2neo.authenticate("46.101.180.63:7474", "neo4j", "uni-bonn")

# Connect to graph and add constraints.
neo4jUrl = os.environ.get('NEO4J_URL',"http://46.101.180.63:7474/db/data/")
#graph = py2neo.Graph(neo4jUrl)

# Add uniqueness constraints.
#graph.run("CREATE CONSTRAINT ON (q:Person) ASSERT q.id IS UNIQUE;")

# Build URL.
apiUrl = "https://api.onename.com/v1/users"
# apiUrl = "https://raw.githubusercontent.com/s-matthew-english/26.04/master/test.json"

# Send GET request.
Allusersjson = requests.get(apiUrl, headers = {"accept":"application/json"}).json()

#print(json)])
UsersDetails=[]
for username in Allusersjson['usernames']:
    usernamex= username[:-3]
    apiUrl2="https://api.onename.com/v1/users/"+usernamex+"?app-id=demo-app-id&amp;app-secret=demo-app-secret"
    userinfo=requests.get(apiUrl2, headers = {"accept":"application/json"}).json()
    # try:
    #     if('bitcoin' not in userinfo[usernamex]['profile']):
    #         continue
    #     else:
    #         UsersDetails.append(userinfo)
    # except:
    #     continue
    try:
        address = userinfo[usernamex]["profile"]["bitcoin"]["address"]
        UsersDetails.append(address)
    except KeyError:
        pass  # no address

out = "\n".join(UsersDetails)
print(out)
open("out.csv", "w").write(out)

# f = csv.writer(open("test.csv", "wb+"))


# Build query.
query = """
RETURN {json}
"""

# Send Cypher query.
# py2neo.CypherQuery(graph, query).run(json=json)
# graph.run(query).run(json=json)

#graph.run(query,json=json)
</code></pre>

<p>anyway, in such a situation, what's the best way to write out those addresses as csv :/</p>

<hr>

<p><strong>UPDATE</strong></p>

<p>I ran it, and at first it worked, but then I got the following error: 
<a href="http://i.stack.imgur.com/E63RX.png" rel="nofollow"><img src="http://i.stack.imgur.com/E63RX.png" alt="enter image description here"></a></p>

<p>Instead of adding all the information to the UsersDetails list</p>

<pre><code>UsersDetails.append(userinfo)
</code></pre>

<p>you can add just the relevant part (address)</p>

<pre><code>try:
    address = userinfo[usernamex]["profile"]["bitcoin"]["address"]
    UsersDetails.append(address)
except KeyError:
    pass  # no address
except TypeError:
    pass  # illformed data
</code></pre>

<p>To print the values to the screen:</p>

<pre><code>out = "\n".join(UsersDetails)  
print(out)
</code></pre>

<p>(replace "\n" with "," for comma separated output, instead of one per line)</p>

<p>To save to a file:</p>

<pre><code>open("out.csv", "w").write(out)
</code></pre>

python json csv bitcoin