首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载上周欧元兑英镑和欧元兑欧元的汇率,并保存在txt文件中。

下载上周欧元兑英镑和欧元兑欧元的汇率,并保存在txt文件中。
EN

Stack Overflow用户
提问于 2022-10-03 15:11:48
回答 1查看 56关注 0票数 -2

每周我都需要用欧元到英镑的汇率生成一个文件,反之亦然。我部分地使用了我在互联网上找到的代码,但我不知道如何让它下载两个表-- EUR到GBP和GBP到EUR。

代码语言:javascript
复制
import requests
import pandas as pd
import io

# Building blocks for the URL
entrypoint = 'https://sdw-wsrest.ecb.europa.eu/service/' # Using protocol 'https'
resource = 'data'           # The resource for data queries is always'data'
flowRef ='EXR'              # Dataflow describing the data that needs to be returned, exchange rates in this case
key = 'D.GBP.EUR.SP00.A'    # Defining the dimension values, D -daily. the currency being masured, the other currency.SP00- type of exchange rates.A- teh series variation
# Define the parameters
parameters = {
    'startPeriod': '2022-09-25',  # Start date of the time series
    'endPeriod': '2022-10-03'     # End of the time series
}

# Construct the URL:
request_url = entrypoint + resource + '/'+ flowRef + '/' + key





response = requests.get(request_url, params=parameters, headers={'Accept': 'text/csv'})

df = pd.read_csv(io.StringIO(response.text))



ts = df.filter(['TIME_PERIOD', 'OBS_VALUE'], axis=1)

ts['TIME_PERIOD'] = pd.to_datetime(ts['TIME_PERIOD'])

ts = ts.set_index('TIME_PERIOD')

table = ts.tail(7)
print(table)
writePath = 'conversion.txt'
with open(writePath, 'a') as f:
    dfAsString = ts.to_string()
    f.write(dfAsString)

更重要的是,该文件以以下形式写入: OBS_VALUE TIME_PERIOD

0.89404 2022-09-27 0.89275 2022-09-28 0.90268 2022-09-29 0.89485 2022-09-30 0.88300 2022-10-03 0.87070

我只需要一个没有“TIME_PERIOD”和“OBS_VALUE”的日期和速率

你能帮我做这个吗?谢谢:)

EN

回答 1

Stack Overflow用户

发布于 2022-10-03 16:01:01

问:-“我只需要一个没有'TIME_PERIOD''OBS_VALUE'的日期和费率”

您的意思是将'TIME_PERIOD'转换为'date',将'OBS_VALUE'转换为'rate'

如果是的话,只需添加1行就可以了:

代码语言:javascript
复制
import requests
import pandas as pd
import io

# Building blocks for the URL
entrypoint = 'https://sdw-wsrest.ecb.europa.eu/service/' # Using protocol 'https'
resource = 'data'           # The resource for data queries is always'data'
flowRef ='EXR'              # Dataflow describing the data that needs to be returned, exchange rates in this case
key = 'D.GBP.EUR.SP00.A'    # Defining the dimension values, D -daily. the currency being masured, the other currency.SP00- type of exchange rates.A- teh series variation
# Define the parameters
parameters = {
    'startPeriod': '2022-09-25',  # Start date of the time series
    'endPeriod': '2022-10-03'     # End of the time series
}

# Construct the URL:
request_url = entrypoint + resource + '/'+ flowRef + '/' + key





response = requests.get(request_url, params=parameters, headers={'Accept': 'text/csv'})

df = pd.read_csv(io.StringIO(response.text))



ts = df.filter(['TIME_PERIOD', 'OBS_VALUE'], axis=1)

ts['TIME_PERIOD'] = pd.to_datetime(ts['TIME_PERIOD'])


ts_c = ts.rename(columns = {'TIME_PERIOD':'date', 'OBS_VALUE':'rate'})#to change 'TIME_PERIOD' to 'date', 'OBS_VALUE' to 'rate'
print(ts_c)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73937442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档