python3将json格式转换为csv格式

[1957, 1934, 2013, 1890, 1974, 1880, 1976, 1944, 1900, 1978, 2002, 1986, 1886, 1951, 1992, 1929, 1965, 1942, 1997, 1981, 1955, 2014, 1954, 1903, 2004, 2001, 2006, 1887, 1990, 1917, 1991, 1950, 1982, 1924, 2005, 1994, 1920, 1995, 1914, 1885, 2007, 1969, 1931, 1933, 1888, 1956, 1898, 1993, 1941, 1904, 1960, 2000, 1947, 1937, 1987, 1983, 1911, 1962, 2003, 1896, 1902, 1881, 2008, 1948, 1884, 1918, 1889, 1926, 1952, 1925, 1928, 1943, 2015, 1967, 1905, 1906, 2009, 1894, 1916, 1945, 1940, 1946, 1891, 1966, 1979, 1923, 2011, 1921, 1975, 1988, 1935, 1953, 1972, 1932, 1895, 1907, 1882, 1971, 1958, 1892, 1963, 1968, 1912, 1989, 1883, 1964, 1977, 1930, 1996, 2010, 1980, 1908, 1910, 1973, 1999, 1939, 1901, 1959, 1949, 1927, 2012, 1938, 1922, 1893, 1897, 1913, 1909, 1919, 1936, 1970, 1915, 1998, 1985, 1961, 1899, 1984]

 

首先我们来简单看一下json的基本知识

JSON(JavaScript Object Notation)

轻量级的数据交换格式

语法规则

       数据是键值对

       由逗号分隔

       {}保存对象{key1:val1,key2:val2}

[]保存数组[val1,val2,…valn]

读操作

Json.load(file_obj)

返回值是dict类型

写操作

Json.dumps()

编码注意ensure_ascii= False, encoding=’utf-8’

我们要转换的json文件是global_temperature.json 在线简单查看一下json文件的内容

导入json,pandas包 定义文件路径

import pandas as pd
import json
jsonfile_path ="./files/global_temperature.json"
#读取json文件
with open(jsonfile_path,"r",encoding="utf-8") as j_obj:
    json_data= json.load(j_obj)
print(json_data)

输出结果

{'data': {'1957': '0.0538', '1934': '-0.1075', '2013': '0.6687', '1890': '-0.3233', '1974': '-0.0698', '1880': '-0.1247', '1976': '-0.0769', '1944': '0.2948', '1900': '-0.0704', '1978': '0.1139', '2002': '0.6018', '1986': '0.2308', '1886': '-0.2101', '1951': '-0.0095', '1992': '0.2583', '1929': '-0.2982', '1965': '-0.0752', '1942': '0.1549', '1997': '0.5185', '1981': '0.3024', '1955': '-0.1305', '2014': '0.7402', '1954': '-0.1118', '1903': '-0.3442', '2004': '0.5806', '2001': '0.5455', '2006': '0.6139', '1887': '-0.2559', '1990': '0.4350', '1917': '-0.3193', '1991': '0.4079', '1950': '-0.1579', '1982': '0.1836', '1924': '-0.2510', '2005': '0.6583', '1994': '0.3420', '1920': '-0.2152', '1995': '0.4593', '1914': '-0.1444', '1885': '-0.2220', '2007': '0.6113', '1969': '0.0937', '1931': '-0.0714', '1933': '-0.2481', '1888': '-0.1541', '1956': '-0.1945', '1898': '-0.2578', '1993': '0.2857', '1941': '0.1974', '1904': '-0.4240', '1960': '0.0252', '2000': '0.4255', '1947': '-0.0455', '1937': '-0.0204', '1987': '0.3710', '1983': '0.3429', '1911': '-0.4367', '1962': '0.0924', '2003': '0.6145', '1896': '-0.0971', '1902': '-0.2535', '1881': '-0.0707', '2008': '0.5415', '1948': '-0.0471', '1884': '-0.2099', '1918': '-0.2118', '1889': '-0.1032', '1926': '-0.0618', '1952': '0.0288', '1925': '-0.1464', '1928': '-0.1749', '1943': '0.1598', '2015': '0.8990', '1967': '-0.0112', '1905': '-0.2967', '1906': '-0.2208', '2009': '0.6354', '1894': '-0.2828', '1916': '-0.2979', '1945': '0.1754', '1940': '0.0927', '1946': '-0.0013', '1891': '-0.2552', '1966': '-0.0204', '1979': '0.2288', '1923': '-0.2161', '2011': '0.5759', '1921': '-0.1517', '1975': '0.0060', '1988': '0.3770', '1935': '-0.1445', '1953': '0.0997', '1972': '0.0280', '1932': '-0.1214', '1895': '-0.2279', '1907': '-0.3767', '1882': '-0.0710', '1971': '-0.0775', '1958': '0.1145', '1892': '-0.3079', '1963': '0.1100', '1968': '-0.0282', '1912': '-0.3318', '1989': '0.2982', '1883': '-0.1481', '1964': '-0.1461', '1977': '0.1996', '1930': '-0.1016', '1996': '0.3225', '2010': '0.7008', '1980': '0.2651', '1908': '-0.4441', '1910': '-0.3862', '1973': '0.1654', '1999': '0.4427', '1939': '-0.0157', '1901': '-0.1471', '1959': '0.0640', '1949': '-0.0550', '1927': '-0.1506', '2012': '0.6219', '1938': '-0.0318', '1922': '-0.2318', '1893': '-0.3221', '1897': '-0.1232', '1913': '-0.3205', '1909': '-0.4332', '1919': '-0.2082', '1936': '-0.1173', '1970': '0.0383', '1915': '-0.0747', '1998': '0.6335', '1985': '0.1357', '1961': '0.0818', '1899': '-0.1172', '1984': '0.1510'}, 'description': {'units': 'Degrees Celsius', 'base_period': '1901-2000', 'title': 'Global Land and Ocean Temperature Anomalies, January-December'}}
#转换keys
year_str_lst= json_data["data"].keys()#取出字段为“data”的keys
year_lst=[int(year_str) for year_str in year_str_lst]#将keys转换为整数 遍历 输出结果为列表
print(year_lst)

输出结果 

[1957, 1934, 2013, 1890, 1974, 1880, 1976, 1944, 1900, 1978, 2002, 1986, 1886, 1951, 1992, 1929, 1965, 1942, 1997, 1981, 1955, 2014, 1954, 1903, 2004, 2001, 2006, 1887, 1990, 1917, 1991, 1950, 1982, 1924, 2005, 1994, 1920, 1995, 1914, 1885, 2007, 1969, 1931, 1933, 1888, 1956, 1898, 1993, 1941, 1904, 1960, 2000, 1947, 1937, 1987, 1983, 1911, 1962, 2003, 1896, 1902, 1881, 2008, 1948, 1884, 1918, 1889, 1926, 1952, 1925, 1928, 1943, 2015, 1967, 1905, 1906, 2009, 1894, 1916, 1945, 1940, 1946, 1891, 1966, 1979, 1923, 2011, 1921, 1975, 1988, 1935, 1953, 1972, 1932, 1895, 1907, 1882, 1971, 1958, 1892, 1963, 1968, 1912, 1989, 1883, 1964, 1977, 1930, 1996, 2010, 1980, 1908, 1910, 1973, 1999, 1939, 1901, 1959, 1949, 1927, 2012, 1938, 1922, 1893, 1897, 1913, 1909, 1919, 1936, 1970, 1915, 1998, 1985, 1961, 1899, 1984]

 

#转换value
temp_str_lst =json_data["data"].values()#取出字段为“data”的values
temp_lst=[float(temp_str) for temp_str in temp_str_lst]#将values转换为浮点数 遍历 输出结果为列表
print(temp_lst)

 输出结果

[0.0538, -0.1075, 0.6687, -0.3233, -0.0698, -0.1247, -0.0769, 0.2948, -0.0704, 0.1139, 0.6018, 0.2308, -0.2101, -0.0095, 0.2583, -0.2982, -0.0752, 0.1549, 0.5185, 0.3024, -0.1305, 0.7402, -0.1118, -0.3442, 0.5806, 0.5455, 0.6139, -0.2559, 0.435, -0.3193, 0.4079, -0.1579, 0.1836, -0.251, 0.6583, 0.342, -0.2152, 0.4593, -0.1444, -0.222, 0.6113, 0.0937, -0.0714, -0.2481, -0.1541, -0.1945, -0.2578, 0.2857, 0.1974, -0.424, 0.0252, 0.4255, -0.0455, -0.0204, 0.371, 0.3429, -0.4367, 0.0924, 0.6145, -0.0971, -0.2535, -0.0707, 0.5415, -0.0471, -0.2099, -0.2118, -0.1032, -0.0618, 0.0288, -0.1464, -0.1749, 0.1598, 0.899, -0.0112, -0.2967, -0.2208, 0.6354, -0.2828, -0.2979, 0.1754, 0.0927, -0.0013, -0.2552, -0.0204, 0.2288, -0.2161, 0.5759, -0.1517, 0.006, 0.377, -0.1445, 0.0997, 0.028, -0.1214, -0.2279, -0.3767, -0.071, -0.0775, 0.1145, -0.3079, 0.11, -0.0282, -0.3318, 0.2982, -0.1481, -0.1461, 0.1996, -0.1016, 0.3225, 0.7008, 0.2651, -0.4441, -0.3862, 0.1654, 0.4427, -0.0157, -0.1471, 0.064, -0.055, -0.1506, 0.6219, -0.0318, -0.2318, -0.3221, -0.1232, -0.3205, -0.4332, -0.2082, -0.1173, 0.0383, -0.0747, 0.6335, 0.1357, 0.0818, -0.1172, 0.151]
#构建dataframe
year_se =pd.Series(year_lst,name ='year')
temp_se =pd.Series(temp_lst,name="temperture")
#j将year_lst和temp_lst转换为一维数组对象 字段名分别为year temperture 
result_df =pd.concat([year_se,temp_se],axis=1)
print(result_df.head())
#利用concat方法将两个series拼接为两列
#concat函数是在pandas底下的方法,可以将数据根据不同的轴作简单的融合
#axis: 需要合并链接的轴,0 是行,1 是列

 输出结果

   year  temperture
0  1957      0.0538
1  1934     -0.1075
2  2013      0.6687
3  1890     -0.3233
4  1974     -0.0698

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值