简体   繁体   English

JSON数据通过python转换为字符串数据

[英]JSON data convert to string data by python

I wanna convert below JSON data to string data same as below.我想将下面的 JSON 数据转换为与下面相同的字符串数据。

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

convert to like this转换成这样

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

^KS11     |20200401|1685.4600   |1737.2800   |1762.4399   |1685.3700  |

If you were provided the json data as a string, I would suggest using the json package, like so:如果您以字符串形式提供 json 数据,我建议您使用json包,如下所示:

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]))

Output:输出:

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

If you didn't want to get the volume ,and it always in the last.You can try this:如果你不想得到volume ,它总是在最后。你可以试试这个:

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)

The result(more one "\\n"):结果(多一个“\\n”):

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

Or one line code:或一行代码:

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

This is(no more "\\n"):这是(不再是“\\n”):

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

(Maybe there is better way) (也许有更好的方法)

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))

assuming you know how to read json using json module.假设您知道如何使用 json 模块读取 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']))

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

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