我正在学习Python,遇到了使用python读取json的问题,并且遇到了以下问题,不知道如何处理这种情况。非常感谢您的帮助。
我正在使用python 3.6,JSON文件格式如下所示
{
"Meta Data": {
"1. Information": "Intraday (5min) open, high, low, close prices and volume",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2019-07-03 13:05:00",
"4. Interval": "5min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (5min)": {
"2019-07-03 13:05:00": {
"1. open": "137.4500",
"2. high": "137.5000",
"3. low": "137.2064",
"4. close": "137.5000",
"5. volume": "0"
},
"2019-07-03 13:00:00": {
"1. open": "137.4800",
"2. high": "137.6000",
"3. low": "137.2400",
"4. close": "137.4500",
"5. volume": "759357"
},
"2019-07-03 12:55:00": {
"1. open": "137.4300",
"2. high": "137.5100",
"3. low": "137.4015",
"4. close": "137.4720",
"5. volume": "371471"
},
"2019-07-03 12:50:00": {
"1. open": "137.3750",
"2. high": "137.4500",
"3. low": "137.2800",
"4. close": "137.4333",
"5. volume": "222396"
},
"2019-07-03 12:45:00": {
"1. open": "137.4100",
"2. high": "137.4700",
"3. low": "137.3200",
"4. close": "137.3727",
"5. volume": "248270"
},
"2019-07-03 12:40:00": {
"1. open": "137.3000",
"2. high": "137.4128",
"3. low": "137.2900",
"4. close": "137.4100",
"5. volume": "177767"
},
"2019-07-03 12:35:00": {
"1. open": "137.3200",
"2. high": "137.3700",
"3. low": "137.2600",
"4. close": "137.3000",
"5. volume": "194017"
},
"2019-07-03 12:30:00": {
"1. open": "137.3797",
"2. high": "137.4250",
"3. low": "137.3100",
"4. close": "137.3100",
"5. volume": "148587"
},
"2019-07-03 12:25:00": {
"1. open": "137.4400",
"2. high": "137.4500",
"3. low": "137.3600",
"4. close": "137.3750",
"5. volume": "163235"
},
"2019-07-03 12:20:00": {
"1. open": "137.2300",
"2. high": "137.4650",
"3. low": "137.1900",
"4. close": "137.4400",
"5. volume": "241906"
},
}
}
我尝试过的代码:
import json
with open("d:/Python Projects/myjason3.json") as f:
j1 = json.load(f)
for data in j1['Time Series (5min)']:
print([data]['1. open'])
我想在每个时间序列值(例如“ 2019-07-03 13:05:00”)下打印键“ 1.open”的值。 我收到的错误是:
print([data]['1. open'])
TypeError: list indices must be integers or slices, not str
答案 0 :(得分:1)
您没有正确引用数据字典。
您应该能够像这样遍历@ComponentScan
键值:
'Time Series (5min)'
答案 1 :(得分:1)
第一个错误来自import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
im_gray = cv2.imread('test/tlqkf10.png', cv2.IMREAD_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 127, 255, cv2.THRESH_TRUNC | cv2.THRESH_OTSU)
chapNum4 = pytesseract.image_to_string(im_bw, lang='eng', config="-psm 8 -oem 3")
print(chapNum4)
:data
的括号。
print(data['1. open'])
创建仅包含一个元素[data]
的列表,而您想直接访问data
字典。
此外,您将在字典上进行迭代,该字典将生成键而不是值;尝试:data
答案 2 :(得分:0)
for data in j1['Time Series (5min)']:
将遍历字典的键(日期),而不遍历实际数据。
[data]['1. open']
转换为(即在第一次迭代中)为["2019-07-03 13:05:00", ]['1. open']
,因此您尝试通过键(["2019-07-03 13:05:00", ]
)访问字符串列表('1. open'
)。
要遍历字典的键和值,Python的方法是使用dict.items()
api:
import json
with open("d:/Python Projects/myjason3.json") as f:
j1 = json.load(f)
for datetime_key, data in j1['Time Series (5min)'].items():
print(data['1. open'])