从csv文件追加字典

时间:2020-05-14 01:58:50

标签: python list dictionary

以下代码将一个更新应用于我的项目。

tagPath = ["Package_PLC/Tags/CCN_CNV01_MX001_A_FLT"]
alarmConfig = {"BLD_CHN01_VS001_A_FLT_C":[["enabled","Value","0"]]}
system.tag.editAlarmConfig(tagPaths, alarmConfig)

我需要做数百遍。

我正在尝试根据csv文件构建tagPathalarmConfig字典。

csv示例:

Equipment,Item Name,Alarm Tag,Alarm Name,Cluster Name,Category,Alarm Desc,Delay,Help,Comment,Variable Tag A,Variable Tag B,Custom 1,Custom 2,Custom 3,Custom 4,Custom 5,Custom 6,Custom 7,Custom 8,Paging,Paging Group,Area,Privilege,Historize,Project,SEQUENCE,TAGGENLINK,EDITCODE,LINKED
"","","BLD_CHN01_VS001_A_FLT_C","BLD_CHN01_VS001_A_FLT_C","","","Catch-up Conveyor / Chain Comms Fault","00:00:00","","BLD_CHN01_VS001_A_FLT_C","BLD_CHN01_VS001_A_FLT_C","KFS_ZNE02_WRM","STUN","","","","","","","","","","1","","","","","","",""
"","","BLD_CHN01_VS001_A_FLT_V","BLD_CHN01_VS001_A_FLT_V","","","Catch-up Conveyor / Chain VSD Fault","00:00:00","","BLD_CHN01_VS001_A_FLT_V","BLD_CHN01_VS001_A_FLT_V","","STUN","","","","","","","","","","1","","","","","","",""
"","","BLD_CHN01_VS001_S_HTY","BLD_CHN01_VS001_S_HTY","","","Catch-up Conveyor / Chain Cicuit Breaker","00:00:00","","BLD_CHN01_VS001_S_HTY","NOT BLD_CHN01_VS001_S_HTY","KFS_ZNE02_WRM","STUN","","","","","","","","","","1","","","","","","",""

这是我到目前为止所拥有的:

import system
import csv

path = system.file.openFile('csv')
if path != None:
    print "path found"
    f=open(path)
    reader = csv.DictReader(f)

    path1 = "Package_PLC/Tags/"
    tagpath = []
    alarmConfig = []
    state = 0
    comment = ""

    for i in reader:

        if row['Alarm Tag'] == 'ECN*' || 'FCN*' || 'PAC*':
            tagpath.append(path1 + int(row['Alarm Tag']))
            alarmname = row[Alarm Tag]

            if row[Variable Tag A] == "NOT*":
                state = 0
            else:
                state = 1

            comment = row[Alarm Desc]
            alarmConfig.append({alarmname: [["setpointA","Value",state],      
                                            ["displayPath","Value","Packing"],
                                            ["notes","Value",comment]]        
                               })                                             
            system.tag.editAlarmConfig(tagPaths, alarmConfig)

    f.close()

抛出以下错误。

Traceback (most recent call last):
  File "<buffer>", line 28, in <module>
TypeError: list indices must be integers

2 个答案:

答案 0 :(得分:0)

这行得通。

import string
import system
import csv

path = system.file.openFile('csv')
if path != None:
    print "path found"
    f=open(path)
    reader = csv.DictReader(f)  

    path1 = "Package_PLC/Tags/"
    tagpath = []
    alarmConfig = {}
    state = 0
    readerlist = list(reader)

    for stuff in readerlist:
        if "PAC" in stuff['Alarm Tag'] or "ECN" in stuff['Alarm Tag'] or "CCN" in stuff['Alarm Tag'] or "FCN" in stuff['Alarm Tag'] :
            tagpath = []
            tagpath.append(str( path1 + stuff['Alarm Tag']))

            if "NOT" in stuff['Variable Tag A']:
                state = 0
            else :
                state = 1

            display = ['displayPath','Value','Packing']

            notes = ['notes','Value',str(stuff['Alarm Desc'])]

            setpointA =['setpointA','Value', str(state)]

            alarmConfig = {}

            alarmConfig[stuff['Alarm Tag']] = [display,notes,setpointA]

            system.tag.editAlarmConfig(tagpath, alarmConfig)

    f.close()

答案 1 :(得分:0)

很难为您提供帮助,因为:

  • 示例文件不会触发任何事情
  • 您未提供system模块

但这仍然是我的尝试:

import os.path
import csv

input_file_name = 'Sample.csv'

if os.path.exists(input_file_name):
    with open(input_file_name, newline='') as input_file:
        events = csv.DictReader(input_file)
        data_extracted = [
            (
                current_event['Alarm Tag'], 
                0 if current_event['Variable Tag A'].startswith('NOT') else 1,
                current_event['Alarm Desc']
            )
            for current_event in events
            if current_event['Alarm Tag'][:3] in ('ECN', 'FCN', 'PAC')
        ]

    tag_paths = [f'Package_PLC/Tags/{x[0]}' for x in data_extracted]
    alarm_config = {
        alarm_name: [
            ['setpointA', 'Value', state],
            ['displayPath', 'Value', 'Packing'],
            ['notes', 'value', comment]
        ]
        for (alarm_name, state, comment) in data_extracted
    }
    system.tag.editAlarmConfig(tag_paths, alarm_config)