Use python requests to download CSV

前端 未结 8 1948
再見小時候
再見小時候 2020-11-29 18:34

Here is my code:

import csv
import requests
with requests.Session() as s:
    s.post(url, data=payload)
    download = s.get(\'url that directly download a          


        
8条回答
  •  情深已故
    2020-11-29 19:15

    You can update the accepted answer with the iter_lines method of requests if the file is very large

    import csv
    import requests
    
    CSV_URL = 'http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv'
    
    with requests.Session() as s:
        download = s.get(CSV_URL)
    
        line_iterator = (x.decode('utf-8') for x in download.iter_lines(decode_unicode=True))
    
        cr = csv.reader(line_iterator, delimiter=',')
        my_list = list(cr)
        for row in my_list:
            print(row)
    

提交回复
热议问题