从包含一些重复元素的 CSV 文件的多维 Python 字典中构建和提取值

Building and extracting values from multidimensional Python dictionary from CSV file with some repeat elements

我有一个如下所示的 CSV 文件。

ServerName,Country,AppID,App,App_Instance,Proc_Status
Server1,SG,AppID1,CUST,Inst1,Running
Server1,SG,AppID2,CUST,Inst2,Running
Server1,SG,AppID3,CUST,Inst3,Running
Server2,SG,AppID1,CUST,Inst4,Running
Server2,SG,AppID2,CUST,Inst5,Running
Server2,SG,AppID3,CUST,Inst6,Running
Server3,SG,AppID1,CUST,Inst7,Running
Server3,SG,AppID2,CUST,Inst8,Running
Server3,SG,AppID3,CUST,Inst9,Down
Server4,SG,AppID1,CUST,Inst10,Running
Server4,SG,AppID2,CUST,Inst11,Running

第一行是header。如您所见,第 1 列到第 4 列中的值在各行中不是唯一的。但这些组合是独一无二的。我的目标是在给定 "ServerName"、"Country"、"AppID" 和 "App".

的情况下找出值 App_instance 和 Proc_Status

如有任何帮助,我们将不胜感激。

我以我有限的知识尝试了嵌套字典是徒劳的。

import csv
from collections import defaultdict

nested_dict = lambda: defaultdict(nested_dict)
nest = nested_dict()

ServerName = nested_dict()
nest.update(ServerName)

Country = nested_dict()
ServerName.update(Country)

AppID = nested_dict()
Country.update(AppID)


    App = nested_dict()
    AppID.update(App)

with open('bim.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            # skip header
            header = row
            line_count +=1
        else:
            for i in range(len(row) ):
                if i == 0 :
                    ServerName.append(row[i]) # AttributeError: "'collections.defaultdict' object has no attribute 'append'"
                elif i == 1 :
                    Country.append(row[i]) #AttributeError: "'collections.defaultdict' object has no attribute 'append'"
                line_count += 1

但我收到 运行 时间错误,即 'append' 不支持 defaultdict

import pandas as pd

df = pd.read_csv()

def server_status(df: pd.DataFrame, ServerName: str, Country: str, AppID: str, App: str) -> pd.DataFrame:
    return df[['App_Instance', 'Proc_Status']][(df.ServerName == ServerName) & (df.Country == Country) & (df.AppID == AppID) & (df.App == App)]

server_status(df, 'Server1', 'SG', 'AppID1', 'CUST')

没有pandas:

import csv
from pathlib import Path

file = Path.cwd() / 'server.csv'  # you can adjust the path to your location

def return_dict(file: Path) -> list:
    with file.open(mode='r') as f:
        list_dict = list(csv.DictReader(f))
    return list_dict


def return_match(file_dict: list, ServerName: str, Country: str, AppID: str, App: str) -> list:
    found = list()
    for row in file_dict:
        if (row['ServerName'] == ServerName) & (row['Country'] == Country) & (row['AppID'] == AppID) & (row['App'] == App):
            found.append(row)
    return found

file_dict = return_dict(file)
matches = return_match(file_dict, 'Server1', 'SG', 'AppID1', 'CUST')

print(matches)

输出:

[OrderedDict([('ServerName', 'Server1'),
              ('Country', 'SG'),
              ('AppID', 'AppID1'),
              ('App', 'CUST'),
              ('App_Instance', 'Inst1'),
              ('Proc_Status', 'Running')])]