python - 使用一些重复元素从 CSV 文件中构建和提取多维 Python 字典中的值

标签 python dictionary multidimensional-array element repeat

我有一个如下所示的 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

第一行是标题。正如您所看到的,第一列到第四列中的值在各行中并不唯一。但这些的组合是独一无二的。我的目标是找出给定“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”

最佳答案

import pandas as pd

df = pd.read_csv()

enter image description here

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')

enter image description here

没有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')])]

关于python - 使用一些重复元素从 CSV 文件中构建和提取多维 Python 字典中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57343227/

相关文章:

python - numpy: fancy indexing "unexpected behaviour"-- fancy indexing 似乎给出的结果是直观预期的 "transpose"

从 Pandas 高效创建字典的 Pythonic 方法

java - 复制二维数组并增加大小以填充新值

python - for循环函数调用文件解析

python - 在嵌入式环境中正确设置 Python home 和 sys.prefix

ios - tableView 数据源的字典

ios - 更正对象和语法以将​​杂项元数据传递给 Swift 中的另一个方法

python - 删除字典列表中分数低的重复项

javascript - 从 Canvas 标签内部分的颜色值返回多维 bool 数组?

c - 纯粹使用指针动态分配二维数组