Use python requests to download CSV

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

    From a little search, that I understand the file should be opened in universal newline mode, which you cannot directly do with a response content (I guess).

    To finish the task, you can either save the downloaded content to a temporary file, or process it in memory.

    Save as file:

    import requests
    import csv
    import os
    
    temp_file_name = 'temp_csv.csv'
    url = 'http://url.to/file.csv'
    download = requests.get(url)
    
    with open(temp_file_name, 'w') as temp_file:
        temp_file.writelines(download.content)
    
    with open(temp_file_name, 'rU') as temp_file:
        csv_reader = csv.reader(temp_file, dialect=csv.excel_tab)
        for line in csv_reader:
            print line
    
    # delete the temp file after process
    os.remove(temp_file_name)
    

    In memory:

    (To be updated)

提交回复
热议问题