我有一个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
没有
pandas
:输出:
相关问题 更多 >
编程相关推荐