I tried one of the sample python code to send email to the active database user as part of the database maintenance procedure. But I have a problem in sending it because of the domain included in the username (e.g. xyz\username)...So, I want to remove the domain name and the backslash and retain only the username as the email list. (Please check the attached python code).
Dan, when I ran the code userList = [user.Name for user in arcpy.ListUsers(workspace)], it returns a list of users with the format 123456\\xyz.com.sa. when I ran your suggested code emails=[name.split("\\")[-1] for name in userList], it returns a list of username only but have a little problem there's a double quote at the end of the username....[u'123456"', u'6789"'].
is user.Name returning the string in the format you showed
why don't you show what the actual emailList looks like minus the "@xyz.com.sa" stuff
because it can't be returning a simple string for user.Name
I suspect that it is returning one of the two incarnations that worked in the sample that follows.
If it does, then you just need to modify the split
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> a <SPAN class="operator token">=</SPAN> <SPAN class="string token">"somedomain\\xyz\\username1"</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> b <SPAN class="operator token">=</SPAN> <SPAN class="string token">"somedomain/xyz/username1"</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> c <SPAN class="operator token">=</SPAN> <SPAN class="string token">"somedomain\xyz\username1"</SPAN> File <SPAN class="string token">"<string>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN> SyntaxError<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">(</SPAN>unicode error<SPAN class="punctuation token">)</SPAN> <SPAN class="string token">'unicodeescape'</SPAN> codec can't decode bytes <SPAN class="keyword token">in</SPAN> position <SPAN class="number token">10</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">11</SPAN><SPAN class="punctuation token">:</SPAN> truncated \xyz escape <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
It can't just be returning a string with a single backslashes in it eitherwise it would throw and error at some point.
Notice that 'a' and 'b' are fine... either 'raw' format, double backslashes or a single forward slash will pass the test, not a single backslash
a <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"somedomain\\xyz\\username1"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"somedomain\\xyz\\username2"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"somedomain\\xyz\\username3"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> emails <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>name<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> name <SPAN class="keyword token">in</SPAN> a<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> emails <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'username1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username2'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username3'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
or...
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> a <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"somedomain/xyz/username1"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"somedomain/xyz/username2"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"somedomain/xyz/username3"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> emails <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>name<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"/"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> name <SPAN class="keyword token">in</SPAN> a<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> emails <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'username1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username2'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username3'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
thanks for the reply...yes that will work but how can I apply it inside the tuple?
Unzipping zips are a pain... so barring the pertinent code snippet... give this a look-see. Note in python the offending string is in 'raw' format, hence the 'r in front of it. A backslash '\' is an escape designator, so to use one without raw format you have to put a double-backslash. Then just split and if the username is the last, just grab the username from the end (-1), if it was the second last, you would use -2 etc
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> a <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"somedomain\xyz\username"</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> b <SPAN class="operator token">=</SPAN> a<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> b <SPAN class="string token">'username'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.