@@ -204,28 +204,20 @@ The Python to "copy to" is very simple, because the HTTP call is a simple get. T
204
204
205
205
``` python
206
206
import requests
207
- import re
208
207
209
208
api_key = {api_key}
210
209
username = {api_key}
211
- download_file = ' upload_example_dl .csv'
210
+ download_filename = ' download_example .csv'
212
211
q = " COPY upload_example (the_geom, name, age) TO stdout WITH (FORMAT csv, HEADER true)"
213
212
214
- # request the download, specifying desired file name
213
+ # request the download
215
214
url = " http://%s .carto.com/api/v2/sql/copyto" % username
216
- r = requests.get(url, params = {' api_key' : api_key, ' q' : q, ' filename ' : download_file }, stream = True )
215
+ r = requests.get(url, params = {' api_key' : api_key, ' q' : q}, stream = True )
217
216
r.raise_for_status()
218
217
219
- # read save file name from response headers
220
- d = r.headers[' content-disposition' ]
221
- savefilename = re.findall(" filename=(.+)" , d)
222
-
223
- if len (savefilename) > 0 :
224
- with open (savefilename[0 ], ' wb' ) as handle:
225
- for block in r.iter_content(1024 ):
226
- handle.write(block)
227
- print (" Downloaded to: %s " % savefilename)
228
- else :
229
- print (" Error: could not find read file name from headers" )
218
+ with open (download_filename, ' wb' ) as handle:
219
+ for block in r.iter_content(1024 ):
220
+ handle.write(block)
221
+ print (" Downloaded to: %s " % savefilename)
230
222
```
231
223
0 commit comments