尝试遍历嵌套字典并写入csv-Python 2

时间:2019-01-23 20:07:58

标签: python python-2.7 csv dictionary

我有两个csv。一个csv包含用户数据,一个csv包含公司数据。用户数据中的每个人都会有一个company_code。公司数据中的每个公司还将有一个company_code。用户数据中的每个人都与特定的公司ID相关联。

目标是创建一个csv,列出从最新到最旧的公司。我想针对每个每家公司显示未登录用户的百分比和数量,不到一周前,不到2周前,少于1个用户的登录百分比一个月前,不到2个月前,不到4个月前和超过4个月。

我拥有输出所需的所有计算/数据,我只是在努力遍历companyInfo嵌套字典。我希望 csv输出看起来像:

输出csv的标题 =公司名称,not_logged_in,percent_not_logged_in,percent_within_1_week,percent_within_2_weeks,percent_within_1_month,percent_within_1_month,percent_within_2_months,percent_within_4_months,percent_within

这些行将成为每个公司的汇总数据。

以下是我到目前为止的脚本。

import csv
import time
from datetime import datetime, timedelta, date
import pprint

# This is opening userdata csv and reading it in as a list.
with open('user_data_outreach.csv') as f:
    reader = csv.reader(f)
    next(reader)
    rawUserData = list(reader)
    userData = []

    # This is creating a dictionary within the empty userData list.
    for user in rawUserData:
        userData.append({
            'first_name': user[0].title(),
            'last_name': user[1].title(),
            'person_id': user[2],
            'company_id': user[3],
            'email': user[4],
            'permission': user[5],
            'accepted_invite': user[6],
            'last_login': user[7]
        })

# This is opening companydata csv and reading it in as a list.
with open('company_data.csv') as f:
    reader = csv.reader(f)
    next(reader)
    rawCompanyData = list(reader)
    companyData = []

    # This is creating a dictionary within the empty companyData list.
    for company in rawCompanyData:
        companyData.append({
            'name': company[0].title(),
            'company_id': company[1],
            'created_at': company[2],
            'has_qr_codes': company[3],
            'has_custom_fields': company[4]
        })

# function used to 
def notLoggedIn():
    companyInfo = {}
    currentTime = datetime.now()
    companyCount = 0
    for company in companyData: 
        companyCount += 1
        companyInfo[company['company_id']] = {
            'name': company['name'],
            'not_logged_in': 0,
            'within_1_week': 0,
            'within_2_weeks': 0,
            'within_1_month': 0,
            'within_2_months': 0,
            'within_4_months': 0,
            'over_4_months': 0,
            'people': 0
        }

    for person in userData:
        if len(person['last_login']) > 0:
            lastLogin = datetime.fromtimestamp(int((person['last_login'])) / 1000)
            td = (currentTime - lastLogin).days
            td = int(td)
            person['days_since_login'] = td

    for person in userData:
        if person['company_id'] in companyInfo:
            if person['last_login'] == '':
                companyInfo[person['company_id']]['not_logged_in'] += 1
                companyInfo[person['company_id']]['people'] += 1
            elif person['days_since_login'] < 7:
                companyInfo[person['company_id']]['within_1_week'] += 1
                companyInfo[person['company_id']]['within_2_weeks'] += 1
                companyInfo[person['company_id']]['within_1_month'] += 1
                companyInfo[person['company_id']]['within_2_months'] += 1
                companyInfo[person['company_id']]['within_4_months'] += 1
                companyInfo[person['company_id']]['people'] += 1
            elif 7 <= person['days_since_login'] < 14:
                companyInfo[person['company_id']]['within_2_weeks'] += 1
                companyInfo[person['company_id']]['within_1_month'] += 1
                companyInfo[person['company_id']]['within_2_months'] += 1
                companyInfo[person['company_id']]['within_4_months'] += 1
                companyInfo[person['company_id']]['people'] += 1
            elif 14 <= person['days_since_login'] < 31:
                companyInfo[person['company_id']]['within_1_month'] += 1
                companyInfo[person['company_id']]['within_2_months'] += 1
                companyInfo[person['company_id']]['within_4_months'] += 1
                companyInfo[person['company_id']]['people'] += 1
            elif 31 <= person['days_since_login'] < 62:
                companyInfo[person['company_id']]['within_2_months'] += 1
                companyInfo[person['company_id']]['within_4_months'] += 1
                companyInfo[person['company_id']]['people'] += 1
            elif 62 <= person['days_since_login'] < 120:
                companyInfo[person['company_id']]['within_4_months'] += 1
                companyInfo[person['company_id']]['people'] += 1
            else:
                companyInfo[person['company_id']]['over_4_months'] += 1
                companyInfo[person['company_id']]['people'] += 1

            # Not logged in.
            companyInfo[person['company_id']]['percent_not_logged_in'] = round(100 * (float(
                companyInfo[person['company_id']]['not_logged_in'])) / float(companyInfo[person['company_id']]['people']))

            # Within 1 week.
            companyInfo[person['company_id']]['percent_within_1_week'] = round(100 * (float(
                companyInfo[person['company_id']]['within_1_week'])) / float(companyInfo[person['company_id']]['people']))

            # Within 2 weeks.
            companyInfo[person['company_id']]['percent_within_2_weeks'] = round(100 * (float(
                companyInfo[person['company_id']]['within_2_weeks'])) / float(companyInfo[person['company_id']]['people']))

            # Within 1 month.
            companyInfo[person['company_id']]['percent_within_1_month'] = round(100 * (float(
                companyInfo[person['company_id']]['within_1_month'])) / float(companyInfo[person['company_id']]['people']))

            # Within 2 months.
            companyInfo[person['company_id']]['percent_within_2_months'] = round(100 * (float(
                companyInfo[person['company_id']]['within_2_months'])) / float(companyInfo[person['company_id']]['people']))

            # Within 4 months.
            companyInfo[person['company_id']]['percent_within_4_months'] = round(100 * (float(
                companyInfo[person['company_id']]['within_4_months'])) / float(companyInfo[person['company_id']]['people']))

            # Over 4 months.
            companyInfo[person['company_id']]['percent_over_4_months'] = round(100 * (float(
                companyInfo[person['company_id']]['over_4_months'])) / float(companyInfo[person['company_id']]['people']))


notLoggedIn()

因此剩下的就是将数据写入csv。

我知道这个脚本可能很笨拙,但是我正在学习!

在此先感谢您的帮助!

0 个答案:

没有答案