Py学习  »  Python

python从csv返回特定时间在特定服务器下使用许可证的次数

Ashish Gupta • 5 年前 • 1485 次点击  

我有一个csv文件,名为user_log.csv。我需要计算在特定服务器上和每个特定时间使用每个许可证的次数。 日志文件csv如下:

Report Date Time,Server,Licence,User,Host,In Use Since Date Time
02/02/2019 11:50,server1,teamcenter_author,user115,TcServer,02/02/2019 11:48
02/02/2019 11:50,server1,teamcenter_consumer,user133,TcServer,02/02/2019 08:57
02/02/2019 11:50,server2,teamcenter_consumer,user31,TcServer,02/02/2019 10:07
02/02/2019 11:50,server1,teamcenter_consumer,user120,TcServer,02/02/2019 10:15
02/02/2019 11:50,server1,visview_base,user120,client1,02/02/2019 05:14
02/02/2019 11:50,server1,visview_base,user12,client2,02/02/2019 07:19
02/02/2019 11:50,server1,visview_base,user95,client3,02/02/2019 11:10
02/02/2019 11:50,server3,NX12100N,user70,client4,02/01/2019 10:54
02/02/2019 11:50,server3,NX12100N_gateway,user70,client4,02/01/2019 10:54
02/02/2019 11:50,server3,NX12100N_solid_modeling,user70,client4,02/01/2019 10:54
02/02/2019 11:55,server1,teamcenter_author,user115,TcServer,02/02/2019 11:48
02/02/2019 11:55,server1,teamcenter_consumer,user133,TcServer,02/02/2019 08:57
02/02/2019 11:55,server1,teamcenter_consumer,user31,TcServer,02/02/2019 10:07
02/02/2019 11:55,server1,teamcenter_consumer,user120,TcServer,02/02/2019 10:15
02/02/2019 11:55,server1,visview_base,user120,client1,02/02/2019 05:14
02/02/2019 11:55,server1,visview_base,user12,client2,02/02/2019 07:19
02/02/2019 11:55,server1,visview_base,user95,client3,02/02/2019 11:10
02/02/2019 11:55,server1,visview_base,user115,TcServer,02/02/2019 11:50
02/02/2019 11:55,server3,NX12100N,user70,client4,02/01/2019 10:54
02/02/2019 11:55,server3,NX12100N_gateway,user70,client4,02/01/2019 10:54
02/02/2019 11:55,server3,NX12100N_solid_modeling,user70,client4,02/01/2019 10:54

我写的代码如下

file = open('/user_log.csv', "r")
reader= csv.reader(file)
time={}
for row in reader:
    licence_name = row[2]
    server = row[1]

    if licence_name in time.keys():
        if server in licence_name:
            time[row[2]][row[1]]['count'] += 1
        else:
            time.setdefault(licence_name,{}).setdefault(server,{})['count'] =1
    else:
        time.setdefault(licence_name,{}).setdefault(server,{})['count']=1

return render(request, "stats.html", {'no_of_line':time})

我得到的输出如下:

this 我该怎么做?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/42919
 
1485 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Barmar
Reply   •   1 楼
Barmar    6 年前

license_name 是字符串,不是字典。你想测试一下 server 在嵌套字典中。

你可以使用 if license_name in time ,你不需要使用 time.keys() .

你不需要所有的 setdefault() 打电话来,只要按你的要求创建新字典就行了。你可以用它把所有的代码压缩成一行。

file = open('/user_log.csv', "r")
reader= csv.reader(file)
next(reader, None) # Skip the header line
time={}
for row in reader:
    license_name = row[2]
    server = row[1]

    if licence_name in time:
        if server in time[licence_name]:
            time[licence_name][server]['count'] += 1
        else:
            time[licence_name][server] = {"count": 1}
    else:
        time[licence_name] = { server: { "count": 1 } }

return render(request, "stats.html", {'no_of_line':time})

价值 time 我得到了这个代码和示例csv文件是:

{'NX12100N': {'server3': {'count': 2}},
 'NX12100N_gateway': {'server3': {'count': 2}},
 'NX12100N_solid_modeling': {'server3': {'count': 2}},
 'teamcenter_author': {'server1': {'count': 2}},
 'teamcenter_consumer': {'server1': {'count': 5}, 'server2': {'count': 1}},
 'visview_base': {'server1': {'count': 7}}}