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

2024-03-28 01:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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

第一行是标题。如您所见,第1列到第4列中的值在各行中不是唯一的。但两者的结合是独一无二的。我的目标是找出给定“ServerName”、“Country”、“AppID”和“App”的值App\u instance和Proc\u 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

但是我得到运行时错误,defaultdict不支持append


Tags: csvappsgcountryrunningdictappidrow
1条回答
网友
1楼 · 发布于 2024-03-28 01:22:32
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')])]

相关问题 更多 >