mardi 11 août 2020

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

I tried to extract a set of date values from a json input. And when i finished the extraction of F_Date , it was correct.

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

I set a list to contain the values, so I wanna use the index of list like List[0]to take whatever the value in list I wanna. However, it failed. It gave me a column of first string. If I try List[1], ofc it gave me a column of 0 instead of 2020-05-20T00:00:00,which is what exactly I want to have. Any Idea what's wrong with my code? I fell confused about what type F_Date is now, it is not a list?

Thanks in advance.

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"
                }
        ]}

Aucun commentaire:

Enregistrer un commentaire