+ 1
How to extract cn value from below list?
I tried with 'for' loop and split and am able extract value, is there alternative way? List=["cn=charan,dc=mysore,dc=com","cn=kiran,dc=mysore,dc=com"]
6 Respostas
+ 3
First, it is not practical to call your actual list as "List" - do not use reserved names as you might overwrite the standard functionality that is associated with the original keyword.
Second, it may be more practical to use nested dictionaries inside your list. then it is much easier to reference the "cn" values.
Third, if you stick to the original list, you may use also regular expressions to parse the strings.
Examples to both methods are in the attached code.
https://code.sololearn.com/co8IAThe2qgI/#py
+ 2
Thank you very muchTibor Santa
+ 1
Hello Raj
I did some changes to your code so that it will work with regular expression matching. (your solution should be fine as well.)
https://code.sololearn.com/c3T13uvD7odB/#py
import re
for row in rows:
if row.get("mail"):
Emaillist += [re.match("[^@]+", row["mail"]).group(0)]
if row.get("distinguishedName"):
Namelist += [re.match("CN=([^,]+)", row["distinguishedName"]).group(1)]
+ 1
Ok so that is new information that the distinguishedName does not always contain the substring that you want to extract from it.
So just change the second if statement to also check for that, before the regexp is executed. Like this:
if "CN=" in row.get("distinguishedName"):
0
hello Tibor Santa
Can you please help me to extract CN name values from dictionary more efficient ways.
I have not uploaded entire code here
https://code.sololearn.com/c2M9Kj87q8Zr/?ref=app
0
Superr Tibor Santa and thank you
https://code.sololearn.com/c9fu2mNaU7H8/?ref=app
This code not executing if 'CN=' is not present
Please correct me