我有一个配置文件(我使用Python的ConfigParser读取的.txt文件),稍后我将其作为员工姓名缩写的列表读取到Python中,如下所示:
[CSE]
MB = Mike
SI = Samantha
TH = Tom
...And so on. The length of the config file is prone to change as more or less people are added.我的程序必须读取excel电子表格。我使用Openpyxl。在电子表格中有数百行信息(每行代表一个事件)。有四个类别,每个类别都有多个项目。(数据、wifi、语音、电缆)。对于每个员工,我必须报告类别,然后每个月完成事件。我对如何存储每个员工的信息感到困惑,因为配置文件列表可能会更改(按长度或按员工)?我的主管建议在类中为每个员工创建一个新对象,但我如何存储信息并引用每个对象?
提前谢谢你!
发布于 2020-07-28 05:08:14
我没有正确理解你的主要问题,但从我上次的Python评估中,我必须从syslog.log生成html报告,其中每一行都包含每个用户的几个信息。最好的方法是首先只将用户名附加到usernames.csv文件中,然后将所有不同的信息保存在columns.csv文件中,这可以使用正则表达式和运算符模块来完成。一旦你有了正确的脚本来生成2个提到的csv文件,所有其余的都将是孩子们的游戏。如果你想让我给你发送我的脚本评估,你告诉我,如果它没有帮助,它会给你提示。
发布于 2020-07-28 06:22:16
这是syslog.log的内容:
Jan 31 00:09:39 ubuntu.local ticky: INFO Created ticket [#4217] (mdouglas)
Jan 31 00:16:25 ubuntu.local ticky: INFO Closed ticket [#1754] (noel)
Jan 31 00:21:30 ubuntu.local ticky: ERROR The ticket was modified while updating (breee)
Jan 31 00:44:34 ubuntu.local ticky: ERROR Permission denied while closing ticket (ac)
Jan 31 01:00:50 ubuntu.local ticky: INFO Commented on ticket [#4709] (blossom)
Jan 31 01:29:16 ubuntu.local ticky: INFO Commented on ticket [#6518] (rr.robinson)
Jan 31 01:33:12 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mcintosh)
Jan 31 01:43:10 ubuntu.local ticky: ERROR Tried to add information to closed ticket (jackowens)
Jan 31 01:49:29 ubuntu.local ticky: ERROR Tried to add information to closed ticket (mdouglas)
Jan 31 02:30:04 ubuntu.local ticky: ERROR Timeout while retrieving information (oren)
Jan 31 02:55:31 ubuntu.local ticky: ERROR Ticket doesn't exist (xlg)
Jan 31 03:05:35 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller)
Jan 31 03:08:55 ubuntu.local ticky: ERROR Ticket doesn't exist (blossom)
Jan 31 03:39:27 ubuntu.local ticky: ERROR The ticket was modified while updating (bpacheco)
Jan 31 03:47:24 ubuntu.local ticky: ERROR Ticket doesn't exist (enim.non)
Jan 31 04:30:04 ubuntu.local ticky: ERROR Permission denied while closing ticket (rr.robinson)
Jan 31 04:31:49 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren)
Jan 31 04:32:49 ubuntu.local ticky: ERROR Timeout while retrieving information (mcintosh)
Jan 31 04:44:23 ubuntu.local ticky: ERROR Timeout while retrieving information (ahmed.miller)
Jan 31 04:44:46 ubuntu.local ticky: ERROR Connection to DB failed (jackowens)
Jan 31 04:49:28 ubuntu.local ticky: ERROR Permission denied while closing ticket (flavia)
Jan 31 05:12:39 ubuntu.local ticky: ERROR Tried to add information to closed ticket (oren)
Jan 31 05:18:45 ubuntu.local ticky: ERROR Tried to add information to closed ticket (sri)
Jan 31 05:23:14 ubuntu.local ticky: INFO Commented on ticket [#1097] (breee)
Jan 31 05:35:00 ubuntu.local ticky: ERROR Connection to DB failed (nonummy)
Jan 31 05:45:30 ubuntu.local ticky: INFO Created ticket [#7115] (noel)
Jan 31 05:51:30 ubuntu.local ticky: ERROR The ticket was modified while updating (flavia)
Jan 31 05:57:46 ubuntu.local ticky: INFO Commented on ticket [#2253] (nonummy)
Jan 31 06:12:02 ubuntu.local ticky: ERROR Connection to DB failed (oren)
Jan 31 06:26:38 ubuntu.local ticky: ERROR Timeout while retrieving information (xlg)
Jan 31 06:32:26 ubuntu.local ticky: INFO Created ticket [#7298] (ahmed.miller)
Jan 31 06:36:25 ubuntu.local ticky: ERROR Timeout while retrieving information (flavia)
Jan 31 06:57:00 ubuntu.local ticky: ERROR Connection to DB failed (jackowens)
Jan 31 06:59:57 ubuntu.local ticky: INFO Commented on ticket [#7255] (oren)
Jan 31 07:59:56 ubuntu.local ticky: ERROR Ticket doesn't exist (flavia)
Jan 31 08:01:40 ubuntu.local ticky: ERROR Tried to add information to closed ticket (jackowens)
Jan 31 08:03:19 ubuntu.local ticky: INFO Closed ticket [#1712] (britanni)
Jan 31 08:22:37 ubuntu.local ticky: INFO Created ticket [#2860] (mcintosh)
Jan 31 08:28:07 ubuntu.local ticky: ERROR Timeout while retrieving information (montanap)这是我的评估代码,它将创建两个文件: error_message.csv,其中将计算每个不同的错误日志条目。user_statistics.csv,其中将创建3列如下:用户,信息,错误,每一列将根据每个用户日志活动填充。
#!/usr/bin/env python3
import re
import csv
import operator
import sys
per_user = {}
errors = {}
logfile = 'syslog.log'
f = open(logfile, 'r')
errorfile = 'error_message.csv'
userfile = 'user_statistics.csv'
for log in f:
result = re.search(r"ticky: ([\w+]*):? ([\w' ]*) [\[[0-9#]*\]?]? ?\((.*)\)$", log)
if result.group(2) not in errors.keys():
errors[result.group(2)] = 0
errors[result.group(2)] += 1
if result.group(3) not in per_user.keys():
per_user[result.group(3)] = {}
per_user[result.group(3)]["INFO"] = 0
per_user[result.group(3)]["ERROR"] = 0
if result.group(1) == "INFO":
per_user[result.group(3)]["INFO"] += 1
elif result.group(1) == "ERROR":
per_user[result.group(3)]["ERROR"] += 1
errors = sorted(errors.items(), key = operator.itemgetter(1), reverse = True)
per_user = sorted(per_user.items())
f.close()
errors.insert(0, ('Error', 'Count'))
f = open(errorfile, 'w')
for error in errors:
a,b = error
f.write(str(a)+','+str(b)+'\n')
f.close()
f = open(userfile, 'w')
f.write("Username,INFO,ERROR\n")
for stats in per_user:
a, b = stats
f.write(str(a)+','+str(b["INFO"])+','+str(b["ERROR"])+'\n')现在,您可以通过将它们转换为HTML页来可视化__error_message.csv __and user_statistics.csv。要执行此操作,请像我们在上一节中所做的那样,将文件逐个传递到script csv_to _html.py文件。
首先,授予将托管该HTML页面的目录的写权限:
sudo chmod o+w /var/www/html然后,要将error_message.csv转换为HTML文件,请运行以下命令:
./csv_to_html.py error_message.csv /var/www/html/<html-filename>.html替换为您选择的名称。
要将user_statistics.csv转换为HTML文件,请执行以下命令:
./csv_to_html.py user_statistics.csv /var/www/html/<html-filename>.html替换为新名称
现在,要查看这些HTML页面,请打开任何web浏览器,然后使用浏览器打开html页面。
在通过评估后,我了解到在许多复杂的情况下,创建新文件并附加匹配的正则表达式是拥有一份舒适工作的最佳方法。
希望它能在这项任务或其他任务中对你有所帮助。
https://stackoverflow.com/questions/63123425
复制相似问题