Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, json
- all_data = []
- for file in os.listdir('.'):
- print(file)
- if file == 'media.py':
- continue
- with open(file, encoding='utf8') as f:
- i = 0
- perUser = 0
- data = json.load(f)
- #fazer por milhao page unica view por usuario
- data = data['Awis']['Results']['Result']['Alexa']['TrafficHistory']
- if data['HistoricalData'] is None:
- continue
- if type(data['HistoricalData']['Data']) == list:
- i += 1
- for _data in data['HistoricalData']['Data']:
- perUser += float(_data['PageViews']['PerUser'])
- #means that are only one record to retrieve
- else:
- i = 1
- perUser += float(data['HistoricalData']['Data']['PageViews']['PerUser'])
- all_data.append({'name':file, 'media': perUser/i})
- print (all_data)
- import csv
- # Write CSV Header, If you dont need that, remove this line
- with open('dados.csv', mode='w') as csv_file:
- fieldnames = ['name', 'media']
- writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
- writer.writeheader()
- for x in all_data:
- print(x)
- writer.writerow(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement