[英]data extraction from textfile in python 3
我很難通過文本文件將最后的數據提取到程序中。我對Python還是很陌生,如果答案很簡單,請原諒。
我在文本文件中設置了此數據序列,我已經提取了美元作為我的銷售貨幣英鎊作為我的購買貨幣,並提取了0.50443作為我的匯率,但是我不知道如何提取成本作為我的交易成本和0.0001作為該變量的相關值。
{"USD_GBP_COST": "0.50443,0.0001", "USD_USD_COST": "1.00000,0.0000", "USD_EUR_COST": "0.73951,0.01211"}
以下是我其他部分的代碼:
currency_rates = json.loads(page)
splited_rates = re.compile("([A-Z]{3})_([A-Z]{3})")#split the string which is read from the url,it should be any 3 uppercase characters sperated by a _
for key in currency_rates:
matches=splited_rates.match(key)
log_con_rate = -math.log(float(currency_rates[key]))
selling_currency = matches.group(1).encode('ascii','ignore')
buying_currency = matches.group(2).encode('ascii','ignore')
問題在這里:
float(currency_rates[key])
這給出了錯誤ValueError: could not convert string to float: '0.50443,0.0001'
。 我認為該信息是不言自明的。 它告訴您在將字符串轉換為float
之前,需要將字符串分成兩個數字:
con_rate_s, transaction_cost_s = currency_rates[key].split(",")
log_con_rate = -math.log(float(con_rate_s))
transaction_cost = float(transaction_cost_s)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.