Dúvidas Array Python

Asked

Viewed 122 times

0

I have a script that takes all the Windows Services and filters only those that have MSSQL and then take the name of the services as image below: inserir a descrição da imagem aqui

But I need to take the values of the Array and compare with a specific value (this is my problem) when print the array and the location I want (Ws1) he gives me that return:

inserir a descrição da imagem aqui

My question is: The pq this occurring being that I’m taking the 1 house of the array?

Follows the code

import psutil, re
import numpy as np 
#with open('C:\Zabbix\Install\Texto.txt', 'w+') as arq:
#    arq.writelines (str(list(psutil.win_service_iter())))
s =str(list(psutil.win_service_iter()))
s = s.split()
x = 0
y = 0
j = 0
ws = []
for item in s: 
    if 'MSSQL' in s[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

1 answer

0

The problem is on this line:

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

The str() ali is converting the result into a string! So when you access ws[1] is taking the second character of this string (the first would be ws[0]).

See also that further up in

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

and in

v = str(s[x])

You’re also converting the list into a string! This makes you have to give split, remove parentheses with `.replace(), etc - all this would not be necessary if you simply used the list object directly - the parentheses are only generated because you are asking python to make a representation of your object in string form.

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

  • Got it though, when I shoot I can’t capture the values by being a class of the LIB PSUTIL... that’s what "broke me," so I’m kind of forced to do this

  • @Wallacebrunogentil classes of this lib can have their values captured yes... Try to inspect the class, use the command dir() or help(), look for examples - convert to string is the wrong way to do and will bring you trouble every time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.