Doubts Array Python

0

I have a script that takes all Windows Services and filters only the ones that have MSSQL and then I get seed the name of the services as shown below:

ButIneedtogetArrayvaluesandcomparethemwithaspecificvalue(thisismyproblemhere)whenIprintthearrayandthelocationIwant(ws1 ) it gives me this return:

Myquestionis:WhyisthisoccurringwhenI'mtaking1arrayhome?

Followthecode

importpsutil,reimportnumpyasnp#withopen('C:\Zabbix\Install\Texto.txt','w+')asarq:#arq.writelines(str(list(psutil.win_service_iter())))s=str(list(psutil.win_service_iter()))s=s.split()x=0y=0j=0ws=[]foritemins:if'MSSQL'ins[x]:v=str(s[x])v=v.replace('(','')v=v.replace(')','')v=v.split("'")
        ws = str(list(filter(lambda w: w.startswith('MSSQL'), v)))
        print(ws[1])

    x += 1
    
asked by anonymous 07.08.2018 / 21:11

1 answer

0

The problem lies in this line:

ws = str(list(filter(lambda w: w.startswith('MSSQL'), v)))

The str() there is converting the result to a string! So when you go to ws[1] you are getting the second character of this string (the first one would be ws[0] ).

See also above in

s =str(list(psutil.win_service_iter()))

and in

v = str(s[x])

You are also converting the list to string! This causes you to give split , remove parentheses with '.replace (), etc - all this would not be necessary if you simply used the object list directly - the parentheses are only generated because you are asking python to do a representation of its object in string form.

In python you'd better work with data structures the way they are, rather than converting everything to string. I suggest removing all these str() - this function should only be used when you really need a string.

    
07.08.2018 / 21:25