繁体   English   中英

Python:无法将 json 格式(字典)值放入列表

[英]Python: Cannot put json format(dict) values into a list

我试图从 json 输入中提取一组日期值。 当我完成F_Date的提取时,它是正确的。

2020-05-20T00:00:00
2020-05-18T00:00:00
2020-05-15T00:00:00
2020-05-13T00:00:00

我设置了一个列表来包含这些值,所以我想使用List[0]之类的列表索引来获取列表中的第一个值或列表中的其他值以用于进一步的目的。 然而,它失败了。 它给了我一列第一个字符。 如果我尝试List[1] ,它给了我一列0而不是2020-05-18T00:00:00 ,这正是我想要的。 知道我的代码有什么问题吗? 我对F_Date现在是什么类型感到困惑,它不是列表吗?

提前致谢。

F_Date = []
for cell in json["Cells"]:
    if cell["ColumnName"] == "T":
        if cell["RowIndex"] + 1 > 9:
            F_Date = cell["DisplayValue"]
            print(F_Date[0])


output:
2
2
2
2

Json:

json =    {
            "SheetName": "price",
            "SheetIndex": 4,
            "Cells": [
                {
                    "ColumnName": "T",
                    "RowName": "10",
                    "Address": "T10",
                    "ColumnIndex": 19,
                    "RowIndex": 9,
                    "Value": "2020-05-20T00:00:00",
                    "DisplayValue": "2020-05-20T00:00:00",
                    "ValueType": "Date"
                },
                {
                    "ColumnName": "U",
                    "RowName": "10",
                    "Address": "U10",
                    "ColumnIndex": 20,
                    "RowIndex": 9,
                    "Value": 2.75,
                    "DisplayValue": 2.75,
                    "ValueType": "Numeric"
                },
                {
                    "ColumnName": "V",
                    "RowName": "10",
                    "Address": "V10",
                    "ColumnIndex": 21,
                    "RowIndex": 9,
                    "Value": 2.15,
                    "DisplayValue": 2.15,
                    "ValueType": "Numeric"
                },
                {
                    "ColumnName": "T",
                    "RowName": "11",
                    "Address": "T11",
                    "ColumnIndex": 19,
                    "RowIndex": 10,
                    "Value": "2020-05-18T00:00:00",
                    "DisplayValue": "2020-05-18T00:00:00",
                    "ValueType": "Date"
                },
                {
                    "ColumnName": "U",
                    "RowName": "11",
                    "Address": "U11",
                    "ColumnIndex": 20,
                    "RowIndex": 10,
                    "Value": 2.75,
                    "DisplayValue": 2.75,
                    "ValueType": "Numeric"
                },
                {
                    "ColumnName": "V",
                    "RowName": "11",
                    "Address": "V11",
                    "ColumnIndex": 21,
                    "RowIndex": 10,
                    "Value": 2.15,
                    "DisplayValue": 2.15,
                    "ValueType": "Numeric"
                },
                {
                    "ColumnName": "T",
                    "RowName": "12",
                    "Address": "T12",
                    "ColumnIndex": 19,
                    "RowIndex": 11,
                    "Value": "2020-05-15T00:00:00",
                    "DisplayValue": "2020-05-15T00:00:00",
                    "ValueType": "Date"
                },
                {
                    "ColumnName": "U",
                    "RowName": "12",
                    "Address": "U12",
                    "ColumnIndex": 20,
                    "RowIndex": 11,
                    "Value": 2.75,
                    "DisplayValue": 2.75,
                    "ValueType": "Numeric"
                },
                {
                    "ColumnName": "V",
                    "RowName": "12",
                    "Address": "V12",
                    "ColumnIndex": 21,
                    "RowIndex": 11,
                    "Value": 2.15,
                    "DisplayValue": 2.15,
                    "ValueType": "Numeric"
                },
                {
                    "ColumnName": "T",
                    "RowName": "13",
                    "Address": "T13",
                    "ColumnIndex": 19,
                    "RowIndex": 12,
                    "Value": "2020-05-13T00:00:00",
                    "DisplayValue": "2020-05-13T00:00:00",
                    "ValueType": "Date"
                }
        ]}

您需要做的就是打印 F_Date 而不是 F_Date[0] ,它只将第一个字符打印为字符串,可以解释为字符列表,并且您正在打印索引 0。

print(F_Date)

如果您对 F_Date 是什么感到困惑,则 F_Date 是您的日期字符串,因为它是您获得的字典中键的值。

F_Date = []
for cell in json["Cells"]:
    if cell["ColumnName"] == "T":
        if cell["RowIndex"] + 1 > 9:
            F_Date = cell["DisplayValue"] # Here you set F_Date to the value 
            print(F_Date[0])

我想你要做的是这个

F_Date = []
for cell in json["Cells"]:
    if cell["ColumnName"] == "T":
        if cell["RowIndex"] + 1 > 9:
            F_Date.append(cell["DisplayValue"]) 
            print(cell["DisplayValue"])

暂无
暂无

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

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