[英]Split string after certain character
I have this line of code in my python file:我的 python 文件中有这行代码:
included_users = os.environ["INCLUDED_USERS"]
where in one of my config files I have:在我的一个配置文件中,我有:
INCLUDED_USERS = fname.lname:UDWADAW,fname2.lname2:DADWAD,fname3.lname3:DWAFUD,etc.
How can I make it so that I filter the INCLUDED_USERS list to append to an array with just the strings after ":" ?我怎样才能做到这一点,以便我过滤 INCLUDED_USERS 列表以附加到仅包含 ":" 之后的字符串的数组?
so for example I want from the INCLUDED_USERS list:所以例如我想从 INCLUDED_USERS 列表中:
INCLUDED_USERS_ID = ["UDWADAW,DADWAD,DWAFUD"]
Split on commas to get a list of name:value pairs, then split each pair on a colon.用逗号分割以获得名称:值对的列表,然后用冒号分割每对。
for pair in included_users.split(","):
value = pair.split(":")[1]
INCLUDED_USERS_ID.append(value)
Simple list comprehension, but you need to convert included_user to string.简单的列表理解,但您需要将 included_user 转换为字符串。
INCLUDED_USERS = "fname.lname:UDWADAW,fname2.lname2:DADWAD,fname3.lname3:DWAFUD"
INCLUDED_USERS_ID = [ i.split(':')[1] for i in INCLUDED_USERS.split(',')]
print(INCLUDED_USERS_ID)
Output of the above code is上述代码的输出是
['UDWADAW', 'DADWAD', 'DWAFUD']
Hope this helps your case, if not please feel free to comment.希望这对您的情况有所帮助,如果没有,请随时发表评论。 Thansks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.