Use python requests to download CSV

前端 未结 8 1863
再見小時候
再見小時候 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:20

    I use this code (I use Python 3):

    import csv
    import io
    import requests
    
    url = "http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv"
    r = requests.get(url)
    r.encoding = 'utf-8'  # useful if encoding is not sent (or not sent properly) by the server
    csvio = io.StringIO(r.text, newline="")
    data = []
    for row in csv.DictReader(csvio):
        data.append(row)
    
    0 讨论(0)
  • 2020-11-29 19:22

    You can also use the DictReader to iterate dictionaries of {'columnname': 'value', ...}

    import csv
    import requests
    
    response = requests.get('http://example.test/foo.csv')
    reader = csv.DictReader(response.iter_lines())
    for record in reader:
        print(record)
    
    0 讨论(0)
提交回复
热议问题