2 回答

TA貢獻1796條經(jīng)驗 獲得超7個贊
這是一個建議(相當(dāng)冗長以突出顯示正在發(fā)生的事情):
import csv
events = ["PASSED", "FAILED", "EXCEPTION", "NA", "DEPRECATED"]
# Open files
with open('data.csv', 'r') as csv_in, open('data_out.csv', 'w') as csv_out:
# Initialize csv-reader and -writer
csv_reader, csv_writer = csv.reader(csv_in), csv.writer(csv_out)
# Process header
line_in = next(csv_reader)
line_out = line_in + events
csv_writer.writerow(line_out)
# Process data
for line_in in csv_reader:
line_out = line_in
for event in events:
line_out += [sum(1 if event == entry else 0
for entry in line_in[1:])]
csv_writer.writerow(line_out)
我假設(shè)您的數(shù)據(jù)位于名為data.csv. 你必須調(diào)整這一點。我希望它能起作用...
PS:您的示例數(shù)據(jù)中有拼寫錯誤:DEPRICATED應(yīng)該是DEPRECATED。這會導(dǎo)致非預(yù)期的輸出。
沒有不必要的輔助變量的更緊湊的版本將如下所示:
import csv
events = ["PASSED", "FAILED", "EXCEPTION", "NA", "DEPRECATED"]
with open('data.csv', 'r') as fin, open('data_out.csv', 'w') as fout:
in_, out = csv.reader(fin), csv.writer(fout)
out.writerow(next(in_) + events)
out.writerows(line + [sum(1 if event == entry else 0 for entry in line[1:])
for event in events]
for line in in_)

TA貢獻2021條經(jīng)驗 獲得超8個贊
您可以使用Counter來計算特定單詞的出現(xiàn)次數(shù)。假設(shè)您已經(jīng)打開.csv
文件并存儲在字符串中input
:您可以執(zhí)行以下操作:
from collections import Counter
res_values = ("PASSED", "FAILED", "EXCEPTION", "NA", "DEPRECATED")
input = ("Description,dc1pp1sellv01,dc1pp2sellv01,dc2pp1sellv01\n"
? ? ? ? ?"1.1 Database Placement,PASSED,PASSED,PASSED\n"
? ? ? ? ?"1.2 Use dedicated least privilaged account,PASSED,PASSED,PASSED\n"
? ? ? ? ?"1.3 Diable MySQL history,PASSED,PASSED,FAILED\n"
? ? ? ? ?"2.1 Ensure old passwords is set to 1,PASSED,DEPRICATED,NA")
print('\n'.join(
? ? [line + ',' + ','.join(
? ? ? ? [str(Counter(line.split(','))[res])
? ? ? ? ?if i != 0
? ? ? ? ?else res
? ? ? ? ?for res in res_values]
? ? )
? ? ?for i, line in enumerate(input.split('\n'))]))
我使用列表理解來更好地優(yōu)化流程(因為文件可能非常大),但這里有另一個更清晰的代碼,它執(zhí)行完全相同的操作:
split = input.split('\n')? ? ? ? ? ? ? ? ? ? ? # Split the input line by line
for i, line in enumerate(split):? ? ? ? ? ? ? ?# For each line of the input
? ? if i == 0:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# Write full result name (for the first line)
? ? ? ? split[i] += ',' + ','.join(res_values)
? ? else:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # Count and write result occurrences
? ? ? ? counts = Counter(line.split(','))
? ? ? ? for res in res_values:
? ? ? ? ? ? split[i] += ',' + str(counts[res])
print('\n'.join(split))? ? ? ? ? ? ? ? ? ? ? ? # Join the full string
我提出了一個準(zhǔn)備執(zhí)行的解決方案,但出于優(yōu)化目的,逐行讀取文件當(dāng)然比將其存儲在像這里這樣的字符串變量中更好。
添加回答
舉報