How to extract cn value from below list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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"]

20th Jan 2019, 10:30 AM
Raj Charan
6 Answers
+ 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
20th Jan 2019, 11:12 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Thank you very muchTibor Santa
20th Jan 2019, 12:51 PM
Raj Charan
+ 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)]
20th Jan 2019, 9:48 PM
Tibor Santa
Tibor Santa - avatar
+ 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"):
21st Jan 2019, 5:30 AM
Tibor Santa
Tibor Santa - avatar
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
20th Jan 2019, 1:46 PM
Raj Charan
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
20th Jan 2019, 10:25 PM
Raj Charan