Search code examples
pythonhttpparsingrequesturl-encoding

formatting return from http request python


I'm trying to write a python script which will retrieve values from a http server and change the data into csv format, I am using the httplib2 library to preform the request, but my problem is the return for the content is in application/x-www-form-urlencoded format, I have code for parsing the the data as a dict, but can't seem to do anything with it as this kind of string, I know it might seem simple to extensive users, as I can't seem to find any info on this through research...

below is a sample code, just to illustrate the type of thing I'm trying to do and my problem

import httplib2
http = httplib2.Http()

resp, content = http.request("http://example.com/foo/bar")

Thank you for the Help

btw I'm using python 2.7 and the format the request naturally returns is json


Solution

  • If you are receiving an encoded response, you can use urllib.unquote to replace the escaped characters into their 'actual' representation. Once you do that, you can use the json module to load the string as a Python object and then use the csv module to create a CSV based on the response. The structure of your response will govern how this will be set up on your end, but hopefully this will get you on the right path:

    In [1]: import csv
    
    In [2]: import json
    
    In [3]: import urllib
    
    In [4]: json_resp = urllib.quote('[{"name": "John Doe", "age": 35}, {"name": "Jane Doe", "age": 33}]')
    
    In [5]: json_resp # What I believe your response looks like
    Out[5]: '%5B%7B%22name%22%3A%20%22John%20Doe%22%2C%20%22age%22%3A%2035%7D%2C%20%7B%22name%22%3A%20%22Jane%20Doe%22%2C%20%22age%22%3A%2033%7D%5D'
    
    In [6]: resp = urllib.unquote(json_resp) #'Unquote' the response
    
    In [7]: resp
    Out[7]: '[{"name": "John Doe", "age": 35}, {"name": "Jane Doe", "age": 33}]'
    
    In [8]: content = json.loads(resp) # Turn the resp into a Python object
    
    In [9]: fieldnames = ('name', 'age') # Specify the order in which to write fields
    
    In [10]: with open('mycsv.csv', 'wb') as f:
       ....:     writer = csv.DictWriter(f, fieldnames)
       ....:     writer.writeheader() # Python 2.7+
       ....:     for item in content:
       ....:         writer.writerow(item)
       ....:
       ....:
    

    This will write a CSV that looks like:

    name,age
    John Doe,35
    Jane Doe,33