打开CSV,对特定列进行排序并覆盖现有CSV

时间:2017-01-23 00:18:53

标签: python sorting csv

我被困在这一段时间了一段时间。我尝试打开csv,按严重性排序(严重,高,中,低),然后覆盖现有文件。我也想忽略第一次写入或添加标题行。

原始CSV

IP Address  Severity    Score
10.0.0.1    High        2302
172.65.0.1  Low         310
192.168.0.1 Critical    5402
127.0.0.1   Medium      1672`

修改/排序的CSV

IP Address  Severity    Score
192.168.0.1 Critical    5402
10.0.0.1    High        2302
127.0.0.1   Medium      1672 
172.65.0.1  Low         310

代码

import csv
crit_sev = "Critical"
high_sev = "High"
med_sev = "Medium"
low_sev = "Low"
reader = csv.reader(open('sample.csv', 'r'))
row=0
my_list = []
for row in reader:
    if row[1] == crit_sev:
        my_list.append(row)
    elif row[1] == high_sev:
        my_list.append(row)
    elif row[1] == med_sev:
        my_list.append(row)
    elif row[1] == low_sev:
        my_list.append(row)

writer = csv.writer(open("sample.csv", 'w'))
header = ['IP Address', 'Severity', 'Score']
writer.writerow([header])
for word in my_list:
    writer.writerow([word])

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

这是一个pandas解决方案:

import pandas as pd
# Read the CSV file
data = pd.read_csv('sample.csv')

# Configure the levels of severity
levels = pd.Series({"Critical" : 0, "High" : 1, "Medium" : 2, "Low" : 3})
levels.name='Severity'

# Add numeric severity data to the table
augmented = data.join(levels,on='Severity',rsuffix='_')

# Sort and select the original columns
sorted_df = augmented.sort_values('Severity_')[['IP Address', 'Severity','Score']]

# Overwrite the original file
sorted_df.to_csv('sample.csv',index=False)

答案 1 :(得分:1)

您可以使用Python的csv库执行此操作,如下所示:

import socket     
import csv

severity = {"Critical" : 0, "High" : 1, "Medium" : 2, "Low" : 3}     

with open('sample.csv', 'rb') as f_input:
    csv_input = csv.reader(f_input)
    header = next(csv_input)
    data = sorted(csv_input, key=lambda x: (severity[x[1]], socket.inet_aton(x[0])))

with open('sample.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(header)
    csv_output.writerows(data)

这会保留现有标头,并根据severity列对条目进行排序。接下来它也(可选)使用socket.inet_aton()将IP地址转换为可排序的数字,按IP地址排序(可能对您有用,也可能没用)。

例如:

IP Address,Severity,Score
10.168.0.1,Critical,5402
192.168.0.1,Critical,5402
10.0.0.1,High,2302
127.0.0.1,Medium,1672
172.65.0.1,Low,310