簡體   English   中英

如何為易於更改的配置文件的每個元素存儲信息(PYTHON)

[英]How to store information for each element of a config file that is prone to change (PYTHON)

我有一個配置文件(我使用 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、語音、電纜)。 對於每個員工,我必須報告類別,然后每個月完成事件。 鑒於配置文件列表可能會更改(按長度或按員工),我對如何存儲每個員工的信息感到困惑? 我的主管建議在 class 中為每位員工制作一個新的 object,但我將如何存儲信息並參考每個員工?

先感謝您!

我沒有正確理解您的主要問題,但是從我上次的 Python 評估中,我必須從 syslog.log 生成 html 報告,其中每行包含每個用戶的多個信息。 最好的方法是首先將 append 用戶名放入 usernames.csv 文件中,並將所有不同的信息保存在 columns.csv 文件中,這可以使用正則表達式和運算符模塊完成。 一旦你有正確的腳本來生成提到的 2 個 csv 文件,所有 rest 都將是兒童游戲。 如果你想讓我把我的劇本評估發給你,你告訴我,如果它沒有幫助,它會給你提示。

這是 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)

這是我的評估代碼,它將創建 2 個文件: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')

您現在可以通過將 __error_message.csv __ 和user_statistics.csv轉換為 HTML 頁面來可視化它們。 為此,將文件一個一個地傳遞給腳本 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 頁面,請打開任何網絡瀏覽器並使用瀏覽器打開 html 頁面。

通過此評估后,我了解到在許多復雜的情況下,創建新文件並附加匹配的正則表達式是獲得舒適工作的最佳方法。

希望它可以幫助您完成這項任務或其他任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM