简体   繁体   English

如何从 python 中的字典中删除引号

[英]How to remove quotations from a dictionary in python

Currently, I am using an API to retrieve data about stocks.目前,我正在使用 API 来检索有关股票的数据。 The data comes in the format:数据采用以下格式:

'Monthly Adjusted Time Series': {'2021-04-21': {'1. open': '238.4700', '2. high': '261.4800', '3. low': '238.0501', '4. close': '260.5800', '5. adjusted close': '260.5800', '6. volume': '352378035', '7. dividend amount': '0.0000'}  

and then repeats with new data eg 2021-03-21.然后用新数据重复,例如 2021-03-21。 I then remove the "Monthly Adjusted Time Series" header and send the remaining data to a dictionary as monthly data and define the keys as key_data.然后我删除“每月调整时间序列”header 并将剩余数据作为每月数据发送到字典并将键定义为 key_data。

  date_list = []
data_list =[]
for data in zip(range(12), keys_data):
    data = data[1]
    keys_spec = monthly_data[data]
    date_list.append(data)
    for item in keys_spec:
        if item == "1. open":
            data_list.append(keys_spec[item])
montly_year_dict = dict(zip(date_list, data_list))
print(montly_year_dict)

This returns the data as following这将返回数据如下

{'2021-04-21': '238.4700', '2021-03-31': '235.9000', '2021-02-26': '235.0600', '2021-01-29': '222.5300', '2020-12-31': '214.5100', '2020-11-30': '204.2900', '2020-10-30': '213.4900', '2020-09-30': '225.5100', '2020-08-31': '211.5200', '2020-07-31': '203.1400', '2020-06-30': '182.5400', '2020-05-29': '175.8000'

However, I need the data without the quotation marks to use for ChartJs.但是,我需要不带引号的数据用于 ChartJs。 I've tried several ways through converting into strings, using ast, and splitting the data but nothing has worked thusfar.我已经尝试了几种方法,通过转换为字符串、使用 ast 和拆分数据,但到目前为止没有任何效果。 Is it possible to remove specific characters for a dictionary or is there a better way that doesn't use lists?是否可以删除字典的特定字符,或者是否有更好的方法不使用列表? I am relatively new to python and coding in general so please keep it somewhat simple.我对 python 和一般编码比较陌生,所以请保持简单。 Thanks谢谢

You might want to concatenate the strings into floats您可能希望将字符串连接成浮点数

data_list =[]
for data in zip(range(12), keys_data):
    data = data[1]
    keys_spec = monthly_data[data]
    date_list.append(data)
    for item in keys_spec:
        if item == "1. open":
            data_list.append(keys_spec[item])
montly_year_dict = dict(zip(date_list, float(data_list))) # change it here
print(montly_year_dict)

Here is the documentation I used 是我使用的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM