开发者

Removing characters from a tuple

开发者 https://www.devze.com 2023-04-12 08:51 出处:网络
Im using Users = win32net.NetGroupGetUsers(IP,\'none\',0), to get all the local users on a system. The output is a tuple,

Im using

Users = win32net.NetGroupGetUsers(IP,'none',0),

to get all the local users on a system. The output is a tuple,

(([{'name': u'Administ开发者_开发百科rator'}, {'name': u'Guest'}, {'name': u'Tom'}], 3, 0),)

I want to clean this up so it just prints out "Administrator, Guest, Tom". I tried using strip and replace but you cant use those on tuples. Is there a way to convert this into a string so i can manipulate it or is there an even simpler way to go about it?


This should not end with a comma:

Users = win32net.NetGroupGetUsers(IP,'none',0),  

The trailing comma turns the result into a single item tuple containing the result, which is itself a tuple.

The data you want is in Users[0].

>>> print Users[0]
[{'name': u'Administrator'}, {'name': u'Guest'}, {'name': u'Tom'}]

To unpack this list of dictionaries we use a generator expression:

Users = win32net.NetGroupGetUsers(IP,'none',0)
print ', '.join(d['name'] for d in Users[0])


', '.join(user['name'] for user in Users[0][0])


input = (([{'name': u'Administrator'}, {'name': u'Guest'}, {'name': u'Tom'}], 3, 0),)    
in_list = input[0][0]    
names = [x['name'] for x in in_list]    
print names

[u'Administrator', u'Guest', u'Tom']
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号