我试图拆分字符串的某些部分,以完全隔离字符串的仅一部分。例如,在下面,我试图隔离金佰利公司的公开股价。
我尝试使用procedure TForm1.Button2Click(Sender: TObject);
begin
TabItem1.SetFocus;
end;
,但是这意味着我只能从一侧隔离开盘价。例如,如果看起来像这样,
.split()
我只能使用.split()来获取
'1. open': '131.8600', '2. high': '132.0500', '3. low': '131.7900', '4. close': '132.0400', '5. volume': '76708'}, '2019-11-01 15:55:00':
131.8600','2. high': '132.0500', '3. low': '131.7900', '4. close': '132.0400', '5. volume': '76708'}, '2019-11-01 15:55:00':
r = requests.get(https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=kmb&interval=5min&apikey=exampleapikey)
cont = json.loads(r.content.decode())
cont= str(cont)
ali = cont.split("{")
for x in range(9):
if x!= 1 and x!=0:
print(ali[x]+"\n")
else:
x+=1
我似乎无法弄清楚。对于造成混淆的语言,我们事先表示歉意。
答案 0 :(得分:1)
您真的不应该使用字符串操作。 GET请求以可爱的JSON格式为您提供了可能想要的一切。利用这一点:
def main():
import requests
import json
url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=kmb&interval=5min&apikey=exampleapikey"
response = requests.get(url)
assert response.status_code == 200
data = json.loads(response.text)
open_share_price = data["Time Series (5min)"]["2019-11-01 16:00:00"]["1. open"]
print(open_share_price)
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
输出:
131.8600
答案 1 :(得分:0)
您尝试过这样吗?
r = requests.get("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=kmb&interval=5min&apikey=exampleapikey")
cont = json.loads(r.content.decode())
cont= str(cont)
ali = cont.split("{")
for x in range(9):
if x!= 1 and x!=0:
price=ali[x].split(':')[1].split(',')[0]
print(price+"\n")
else:
x+=1