目录
一,问题背景
二,解决过程
1,csv转json
2,日期格式调整
一,问题背景
做实验时需要对csv格式的源数据进行处理,主要包括转换为json、调整日期格式两个功能,csv原文件内容如下:
CountyName,Date,apparentTemperatureMax,apparentTemperatureMin,cloudCover,dewPoint,humidity,precipIntensity,precipIntensityMax,precipProbability,precipAccumulation,precipTypeIsRain,precipTypeIsSnow,precipTypeIsOther,pressure,temperatureMax,temperatureMin,visibility,windBearing,windSpeed,NDVI,Yield
Adams,11/30/2014 0:00,18.61,-3.01,0,6.77,0.69,0,0,0,0,0,0,0,1027.95,23.93,6.96,10,9,3.8,136.179718,35.6
Adams,12/1/2014 0:00,25.85,11.39,0.1,12.64,0.72,0,0,0,0,0,0,0,1025.38,25.85,11.39,9.82,78,0.88,133.6225433,35.6
Adams,12/2/2014 0:00,21.5,6.74,0.11,15.44,0.77,0,0,0,0,0,0,0,1025.88,29.93,15.54,9.86,4,6.14,133.3864594,35.6
Adams,12/3/2014 0:00,29.05,6.61,0.12,16.71,0.73,0,0,0,0,0,0,0,1017.8,33.57,17.98,9.98,30,5.91,133.2133331,35.6
Adams,12/4/2014 0:00,31.01,20.31,1,24.31,0.83,0.0024,0.0177,0.59,0.495,0,1,0,1016.56,31.01,27.41,7.58,41,4.38,131.176178,35.6
Adams,12/5/2014 0:00,29.9,27.91,0,31.37,0.94,0.0006,0.0061,0.28,0,1,0,0,1021.48,35.08,31.04,5.15,8,3.83,131.6928711,35.6
Adams,12/6/2014 0:00,40.52,28.88,0.11,34.7,0.89,0.0062,0.0366,0.69,0,1,0,0,1020.85,44.7,34.22,7.6,166,1.97,138.5853119,35.6
二,解决过程
1,csv转json
手动编写转换程序也可以,但是这里推荐一个超级方便的在线转换工具Convert CSV to JSON Array - Table Convert Online
界面简洁而且功能强大,无需翻墙!
调整后的格式如下:
?
2,日期格式调整
可以看出Date字段原始格式为【11/30/2014 0:00】,不方便阅读,这里通过python编写脚本,将格式转换为【2014-11-30 00:00】
直接将时间以json格式保存时会报错,所以这里需要添加一个 DateEncoder
代码比较简单。
import time
import csv
import json
import numpy as np
import pandas as pd
from datetime import datetime
def date_transform():
filename = 'json/Adams.json'
target_filename = 'finalJson/Adams1.json'
class DateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime("%Y-%m-%d %H:%M")
else:
return json.JSONEncoder.default(self, obj)
with open(filename, encoding='utf-8') as f1:
data = json.load(f1)
for i in range(len(data)):
data[i]['Date'] = pd.to_datetime(data[i]['Date'], format="%m/%d/%Y %H:%M")
print(data[i]['Date'])
with open(target_filename, 'w', encoding='utf-8') as f2:
json.dump(data, f2, cls=DateEncoder)
f2.close()
f1.close()
|