司馬山哥關(guān)注
1.json模塊介紹
Json 模塊提供了四個(gè)方法: dumps、dump凫佛、loads讲坎、load
詳情參見(jiàn)https://www.cnblogs.com/tjuyuan/p/6795860.html
2 json文件讀取
user = open("user.json", encoding="utf-8").read()
userDict = json.loads(user)
userDict為字典格式的文件,通過(guò)字典操作讀取字段值愧薛。
3 創(chuàng)建數(shù)據(jù)框DataFrame晨炕,便于保存為csv文件
CallCount = pd.DataFrame(
columns=['date_time', 'total_call_count', 'domestic_calls', 'local', 'called', 'dialing', 'max_time',
'max_time_number','frequently_number','frequently_number_calls'])
4 數(shù)據(jù)抽取并統(tǒng)計(jì)字段
for item in userDict["task_data"]["call_info"]:
date_time = item["call_cycle"]
total_call_count = item["total_call_count"]
CallLandType = {}
CallTypeName = {}
CallTime = []
CallOtherNumber = {}
for call in item["call_record"]:
call_land_type = call["call_land_type"]
call_type_name = call["call_type_name"]
call_time = call["call_time"]
call_other_number = call["call_other_number"]
CallLandType[call_land_type] = CallLandType.get(call_land_type, 0) + 1
CallTypeName[call_type_name] = CallTypeName.get(call_type_name, 0) + 1
CallTime.append(call_time)
CallOtherNumber[call_other_number] = CallOtherNumber.get(call_other_number, 0) + 1
MaxTime = max(CallTime)
MaxTimeNumber = item["call_record"][CallTime.index(MaxTime)]["call_other_number"]
FrequentlyNumber = max(CallOtherNumber, key=CallOtherNumber.get)
FrequentlyNumberCalls = CallOtherNumber[FrequentlyNumber]
5 數(shù)據(jù)保存
result = np.array([date_time, total_call_count, CallLandType['國(guó)內(nèi)長(zhǎng)途'], CallLandType['本地通話'], CallTypeName['被叫'],
CallTypeName['主叫'],MaxTime,MaxTimeNumber,FrequentlyNumber,FrequentlyNumberCalls])
CallCount.loc[count] = result
count = count + 1
CallCount.to_csv("callcount.csv",index=False,sep=',')
注釋:CSV2JSON
#-*- coding:utf-8 -*-
import os
import os.path
import csv
rootdir = "/Users/ying/Documents" # folder stores csv files
for parent,dirnames,filenames in os.walk(rootdir):
for filename in filenames:
abs_path = os.path.join(parent,filename)
if ".csv" in abs_path:
print abs_path
#對(duì)每個(gè)文件進(jìn)行處理
with open(abs_path, 'rb') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
header = rows[0]
for rowOne in rows[1:]:
json_row = {}
for i in range(0,len(rowOne)):
json_row[header[i]] = rowOne[i]
print json_row
注:更改csv存儲(chǔ)的文件夾地址,即可方便的將csv轉(zhuǎn)成Python
</article>