Chcę przekonwertować poniższe dane JSON na dane ciągowe, takie same jak poniżej.

   {
         "Time Series (Daily)": {
            "2020-04-02": {
                "1. open": "1693.5300",
                "2. high": "1726.7600",
                "3. low": "1664.1300",
                "4. close": "1724.8600",
                "5. volume": "766313"
            },
            "2020-04-01": {
                "1. open": "1737.2800",
                "2. high": "1762.4399",
                "3. low": "1685.3700",
                "4. close": "1685.4600",
                "5. volume": "1243600"
            }
    }

Przekonwertować na taki

^KS11     |20200402|1664.1300   |1693.5300   |1726.7600   |1724.8600  |

^KS11     |20200401|1685.4600   |1737.2800   |1762.4399   |1685.3700  |
1
younbin shin 2 kwiecień 2020, 17:31

4 odpowiedzi

Najlepsza odpowiedź
import json

data = '''
{
    "Time Series (Daily)": {
        "2020-04-02": {
            "1. open": "1693.5300",
            "2. high": "1726.7600",
            "3. low": "1664.1300",
            "4. close": "1724.8600",
            "5. volume": "766313"
        },
        "2020-04-01": {
            "1. open": "1737.2800",
            "2. high": "1762.4399",
            "3. low": "1685.3700",
            "4. close": "1685.4600",
            "5. volume": "1243600"
        }
    }
}
'''

loaded_data = json.loads(data)
time_series_data = loaded_data["Time Series (Daily)"]
temp = "^KS11"
for date, date_data in time_series_data.items():
    formatted_date = date.replace("-","")
    low_data = date_data["3. low"]
    open_data = date_data["1. open"]
    high_data = date_data["2. high"]
    close_data = date_data["4. close"]
    print("{}\t|{}|{}\t|{}\t|{}\t|{}\t|\n".format(temp, formatted_date, low_data, open_data, high_data, close_data))
1
Dost Arora 2 kwiecień 2020, 14:59

Jeśli dane json zostały dostarczone jako ciąg znaków, sugerowałbym użycie json opakowanie, takie jak:

import json

jdata = """
    {
         "Time Series (Daily)": {
            "2020-04-02": {
                "1. open": "1693.5300",
                "2. high": "1726.7600",
                "3. low": "1664.1300",
                "4. close": "1724.8600",
                "5. volume": "766313"
            },
            "2020-04-01": {
                "1. open": "1737.2800",
                "2. high": "1762.4399",
                "3. low": "1685.3700",
                "4. close": "1685.4600",
                "5. volume": "1243600"
            }
        }
    }"""

def day_to_str(day, data):
    return '|'.join(('^KS11     ',
                    day.replace('-', ''),
                    data['1. open'],
                    data['2. high'],
                    data['3. low'],
                    data['4. close'],
                    ''))

time_series = json.loads(jdata)['Time Series (Daily)']

for day in time_series:
    print(day_to_str(day, time_series[day]))

Wynik:

^KS11     |20200402|1693.5300|1726.7600|1664.1300|1724.8600|
^KS11     |20200401|1737.2800|1762.4399|1685.3700|1685.4600|
2
l_l_l_l_l_l_l_l 2 kwiecień 2020, 14:58

Jeśli nie chciałeś dostać volume, i zawsze na końcu. Możesz spróbować tego:

yourDict = { # assume it is a dict.Json is also OK.
    "Time Series (Daily)": {
        "2020-04-02": {
            "1. open": "1693.5300",
            "2. high": "1726.7600",
            "3. low": "1664.1300",
            "4. close": "1724.8600",
            "5. volume": "766313"
        },
        "2020-04-01": {
            "1. open": "1737.2800",
            "2. high": "1762.4399",
            "3. low": "1685.3700",
            "4. close": "1685.4600",
            "5. volume": "1243600"
        }
    }
}

temp = "^KS11    |{}    |\n"
result = ""

temp = "^KS11    |{}    |\n" # put data into {}
result = "" # use string

# Easy string handle
for i in yourDict["Time Series (Daily)"]:
    # get the value in each dict
    numberList = list(yourDict["Time Series (Daily)"][i].values())[:-1] # extract them
    oneLineNumber = "  |".join(numberList)# make them together
    oneLineStr = temp.format(oneLineNumber) # Add them
    result += oneLineStr
print(result)

Wynik (więcej „\ n”):

^KS11    |20200402|1693.5300    |1726.7600    |1664.1300    |1724.8600    |
^KS11    |20200401|1737.2800    |1762.4399    |1685.3700    |1685.4600    |

Lub jeden kod liniowy:

yourDict = {
    "Time Series (Daily)": {
        "2020-04-02": {
            "1. open": "1693.5300",
            "2. high": "1726.7600",
            "3. low": "1664.1300",
            "4. close": "1724.8600",
            "5. volume": "766313"
        },
        "2020-04-01": {
            "1. open": "1737.2800",
            "2. high": "1762.4399",
            "3. low": "1685.3700",
            "4. close": "1685.4600",
            "5. volume": "1243600"
        }
    }
}

print("\n".join(["^KS11    |{}    |".format(i.replace("-","")+"|"+"    |".join(list(yourDict["Time Series (Daily)"][i].values())[:-1])) for i in yourDict["Time Series (Daily)"]]))

To jest (nie więcej "\ n"):

^KS11    |20200402|1693.5300    |1726.7600    |1664.1300    |1724.8600    |
^KS11    |20200401|1737.2800    |1762.4399    |1685.3700    |1685.4600    |

(Może jest lepszy sposób)

1
jizhihaoSAMA 2 kwiecień 2020, 15:00

Zakładając, że wiesz, jak czytać json za pomocą modułu json.

stocks = {
    "Time Series (Daily)": {
        "2020-04-02": {
            "1. open": "1693.5300",
            "2. high": "1726.7600",
            "3. low": "1664.1300",
            "4. close": "1724.8600",
            "5. volume": "766313"
        },
        "2020-04-01": {
            "1. open": "1737.2800",
            "2. high": "1762.4399",
            "3. low": "1685.3700",
            "4. close": "1685.4600",
            "5. volume": "1243600"
        }
    }
}

for k,v in stocks['Time Series (Daily)'].items():
    print("^KS11\t|{}\t|{}\t|{}\t|{}\t|{}|".format(v['5. volume'],v['3. low'],v['1. open'],v['2. high'],v['4. close']))
-1
abhishek 2 kwiecień 2020, 15:48