when I do curl to a API call link http://example.com/passkey=wedsmdjsjmdd
curl 'http://example.com/passkey=wedsmdjsjmdd'
I get the employee output data on a csv file format, like:
"Steve","421","0","421","2","","","","","","","","","421","0","421","2"
how can parse through this using python.
I tried:
import csv
cr = csv.reader(open('http://example.com/passkey=wedsmdjsjmdd',"rb"))
for row in cr:
print row
but it didn't work and I got an error
http://example.com/passkey=wedsmdjsjmdd No such file or directory:
Thanks!
Using pandas it is very simple to read a csv file directly from a url
This will read your data in tabular format, which will be very easy to process
what you were trying to do with the curl command was to download the file to your local hard drive(HD). You however need to specify a path on HD
You need to replace
open
with urllib.urlopen or urllib2.urlopen.e.g.
This would output the following
To increase performance when downloading a large file, the below may work a bit more efficiently:
By setting
stream=True
in the GET request, when we passr.iter_lines()
to csv.reader(), we are passing a generator to csv.reader(). By doing so, we enable csv.reader() to lazily iterate over each line in the response withfor row in reader
.This avoids loading the entire file into memory before we start processing it, drastically reducing memory overhead for large files.
You could do it with the requests module as well: