Search code examples
pythonzabbix

Formating csv file to utilize zabbix sender


I have a csv file that i need to format first before i can send the data to zabbix, but i enconter a problem in 1 of the csv that i have to use.

This is a example of the part of the file that i have problems:

    Some_String_That_ineed_01[ifcontainsCell01removehere],xxxxxxxxx02
    Some_String_That_ineed_01vtcrmp01[ifcontainsCell01removehere]-[1],aass

so this is 2 lines from the file, the other lines i already treated. i need to check if Cell01 is in the line

    if 'Cell01' in h: do something.

i need to remove all the content beetwen the [ ] included this [] that contains the Cell01 word and leave only this:

    Some_String_That_ineed_01,xxxxxxxxx02
    Some_String_That_ineed_01vtcrmp01-[1],aass

else my script already treats quite easy. There must be a better way then what i think which is use h.split in the first [ then split again on the , then remove the content that i wanna then add what is left sums strings. since i cant use replace because i need this data([1]).

later on with the desired result i will add this to zabbix sender as the item and item key_. I already have the hosts,timestamps,values.


Solution

  • You should use a regexp (re module):

    import re
    
    s = "Some_String_That_ineed_01[ifcontainsCell01removehere],xxxxxxxxx02"
    replaced = re.sub('\[.*Cell01.*\]', '', s)
    
    print (replaced)
    

    will return:

    [root@somwhere test]# python replace.py
    Some_String_That_ineed_01,xxxxxxxxx02
    

    You can also experiment here.