使用Python保存下载的CSV文件

lot*_*man 4 python csv pandas python-requests

我想从带有请求的链接下载csv文件并将其另存为MSFT.csv.但是,我的代码返回错误

文件"<stdin>",第1行,在_csv.Error中:在未引用字段中看到的新行字符 - 您是否需要以通用换行模式打开文件?

import requests
import csv

data=requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo&datatype=csv'
cr = csv.reader(data)

for row in cr:
    print row
Run Code Online (Sandbox Code Playgroud)

我怎么能用它保存MSFT.csv

cs9*_*s95 10

如果您尝试将此数据写入CSV文件,可以先使用下载requests.get.

import csv
import requests

url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo&datatype=csv'
response = requests.get(url)        

with open('out.csv', 'w') as f:
    writer = csv.writer(f)
    for line in response.iter_lines():
        writer.writerow(line.decode('utf-8').split(','))
Run Code Online (Sandbox Code Playgroud)

如果您正在使用pip install --user pandas,则将requests.get内容传递给pip install --user pandas并迭代:

import pandas as pd

df = pd.read_csv(url)   
df.head()

    timestamp    open    high     low   close  adjusted_close    volume  dividend_amount  split_coefficient
0  2019-06-19  135.00  135.93  133.81  135.69          135.69  17946556              0.0                1.0
1  2019-06-18  134.19  135.24  133.57  135.16          135.16  25908534              0.0                1.0
2  2019-06-17  132.63  133.73  132.53  132.85          132.85  14517785              0.0                1.0
3  2019-06-14  132.26  133.79  131.64  132.45          132.45  17821703              0.0                1.0
4  2019-06-13  131.98  132.67  131.56  132.32          132.32  17200848              0.0                1.0

df.to_csv('out.csv')
Run Code Online (Sandbox Code Playgroud)

或者,使用pandas,发送requests.get到缓冲区并传递给它pip install --user pandas.

import csv
import requests

url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo&datatype=csv'
response = requests.get(url)        

with open('out.csv', 'w') as f:
    writer = csv.writer(f)
    for line in response.iter_lines():
        writer.writerow(line.decode('utf-8').split(','))
Run Code Online (Sandbox Code Playgroud)


Sag*_*tha 7

您可以通过请求实现它

import os
import requests

def download_file(url, filename):
    ''' Downloads file from the url and save it as filename '''
    # check if file already exists
    if not os.path.isfile(filename):
        print('Downloading File')
        response = requests.get(url)
        # Check if the response is ok (200)
        if response.status_code == 200:
            # Open file and write the content
            with open(filename, 'wb') as file:
                # A chunk of 128 bytes
                for chunk in response:
                    file.write(chunk)
    else:
        print('File exists')
Run Code Online (Sandbox Code Playgroud)

您可以使用所需的 url 和文件名调用该函数。在您的情况下,它将是:

url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo&datatype=csv'
filename = 'MSFT.csv'
download_file(url, filename)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。