简体   繁体   English

使用Python将Json转换为CSV

[英]Convert Json to CSV using Python

Below, is the json structure I am pulling from my online weather station. 下面是我从在线气象站提取的json结构。 I am also including a json_to_csv python script that is supposed to convert json data to csv output, but only returns a "Key" error. 我还包括一个json_to_csv python脚本,该脚本应该将json数据转换为csv输出,但仅返回“ Key”错误。 I want to pull data from "current_observation": only. 我只想从“ current_observation”中提取数据。

{
  "response": {
  "features": {
  "conditions": 1
  }
    }
  , "current_observation": {
        "display_location": {
        "latitude":"40.466442",
        "longitude":"-85.362709",
        "elevation":"280.4"
        },
        "observation_time_rfc822":"Fri, 26 Jan 2018 09:40:16 -0500",
        "local_time_rfc822":"Sun, 28 Jan 2018 11:22:47 -0500",
        "local_epoch":"1517156567",
        "local_tz_short":"EST",
        "weather":"Clear",
        "temperature_string":"44.6 F (7.0 C)",
    }
}



import csv, json, sys
inputFile = open("pywu.cache.json", 'r') #open json file
outputFile = open("CurrentObs.csv", 'w') #load csv file
data = json.load(inputFile) #load json content 
inputFile.close() #close the input file
output = csv.writer(outputFile) #create a csv.write
output.writerow(data[0].keys())
for row in data:
    output = csv.writer(outputFile) #create a csv.write 
    output.writerow(data[0].keys())
for row in data:
    output.writerow(row.values()) #values row

What's the best method to retrieve the temperature string and convert to .csv format? 检索温度字符串并将其转换为.csv格式的最佳方法是什么? Thank you! 谢谢!

import pandas as pd
df = pd.read_json("pywu.cache.json")
df = df.loc[["local_time_rfc822", "weather", "temperature_string"],"current_observation"].T
df.to_csv("pywu.cache.csv")

maybe pandas can be of help for you. 也许熊猫可以为您提供帮助。 the .read_json() function creates a nice dataframe, from which you can easily choose the desired rows and columns. .read_json()函数创建一个不错的数据框,您可以从中轻松选择所需的行和列。 and it can save as csv as well. 它也可以另存为csv。

to add latitude and longitude to the csv-line, you can do this: 要将纬度和经度添加到csv行中,您可以执行以下操作:

df = pd.read_json("pywu.cache.csv")
df = df.loc[["local_time_rfc822", "weather", "temperature_string", "display_location"],"current_observation"].T
df = df.append(pd.Series([df["display_location"]["latitude"], df["display_location"]["longitude"]], index=["latitude", "longitude"]))
df = df.drop("display_location")
df.to_csv("pywu.cache.csv")

to print the location in numeric values, you can do this: 要以数字值打印位置,可以执行以下操作:

df = pd.to_numeric(df, errors="ignore")
print(df['latitude'], df['longitude'])

This will find all keys (eg "temperature_string") specified inside of the json blob and then write them to a csv file. 这将找到在json blob内部指定的所有键(例如“ temperature_string”),然后将其写入csv文件。 You can modify this code to get multiple keys. 您可以修改此代码以获取多个密钥。

import csv, json, sys

def find_deep_value(d, key):
# Find a the value of keys hidden within a dict[dict[...]]
# Modified from https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-python-dictionaries-and-lists
# @param d dictionary to search through
# @param key to find

    if key in d:
        yield d[key]
    for k in d.keys():
        if isinstance(d[k], dict):
            for j in find_deep_value(d[k], key):
                yield j

inputFile = open("pywu.cache.json", 'r')  # open json file
outputFile = open("mypws.csv", 'w')  # load csv file
data = json.load(inputFile)  # load json content
inputFile.close()  # close the input file
output = csv.writer(outputFile)  # create a csv.write

# Gives you a list of temperature_strings from within the json
temps = list(find_deep_value(data, "temperature_string"))
output.writerow(temps)
outputFile.close()

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

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