每周我都需要用欧元到英镑的汇率生成一个文件,反之亦然。我部分地使用了我在互联网上找到的代码,但我不知道如何让它下载两个表-- EUR到GBP和GBP到EUR。
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”的日期和速率
你能帮我做这个吗?谢谢:)
发布于 2022-10-03 16:01:01
问:-“我只需要一个没有'TIME_PERIOD'和'OBS_VALUE'的日期和费率”
您的意思是将'TIME_PERIOD'转换为'date',将'OBS_VALUE'转换为'rate'
如果是的话,只需添加1行就可以了:
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)https://stackoverflow.com/questions/73937442
复制相似问题