I'm using the active_directory module, and trying to print a list of the users. My code is:
import active_directory as ad
users = ad.AD_Object("LDAP://OU=Home, DC=dome, DC=net")
for user in users.search(objectCategory="Person"):
print str(user)
It prints some of the users until it meets an unicode username. Then it throws the following error:
UnicodeEncodeError: ascii codec
can't encode characthers in position
10-14: ordinaal not in range(128).
What can I do?
Thank you very much.
Try:
print user.decode('utf-8')
Related
I am trying to write the current window to a file. Problem with the code is that it must use an encoding (utf-8) otherwise if a window gets openened like outlook with windowname: Inbox - Outlook - Mail it gives the following error:
UnicodeEncodeError: 'charmap' codec can't encode character '\u200e' in
position 16: character maps to
But when using the utf-8 encoded file, it can not be encoded into base64, this gives the following error(of course):
ValueError: string argument should contain only ASCII characters
Is there a way to encode or encrypt this file(I've used rot-13 which worked and md5 but this didnt work well with reading and decrypting). Or to make the output of q = w.GetWindowText (w.GetForegroundWindow()) not in 'utf-8'.
code:
import win32gui
import time
import psutil
import win32process
i = 0
while i <= 1:
time.sleep(2)
w=win32gui
q = w.GetWindowText (w.GetForegroundWindow())
q =str(q)
print(q)
pid = win32process.GetWindowThreadProcessId(w.GetForegroundWindow())
print(psutil.Process(pid[-1]).name())
with open("lolp.txt",'w',encoding='utf-8')as f:
f.write(q)
As far as I can tell you need to remove non-ascii symols from q. It can be done in many ways. For example:
import win32gui
q = win32gui.GetWindowText(win32gui.GetForegroundWindow())
def remove_non_ascii(char):
return 32 <= ord(char) <= 126
q = filter(remove_non_ascii, q)
print("".join(list(q)))
Here are another solutions: How can I remove non-ASCII characters but leave periods and spaces?
I came across following code to decode a password string, for example my password string is 'samplepassword', i can encode this using base64 algorithm and i got the below encoded value. I just used https://io/Utils/Base64/ to find encoded value.
"c2FtcGxlcGFzc3dvcmQ="
Below code hides the exact password of mine which is 'samplepassword', but anyone using the encodedvalue can easily find the original password using the same https://io/Utils/Base64/.
I'm confused in understanding what security base64 module providing, and Please suggest some best practices to hide the password in the python code.
def decode(encoded_value):
try:
import base64
try:
decoded_value = base64.b64decode(encoded_value).decode('ascii')
return decoded_value
except TypeError as e:
raise TypeError("Attempted to decode {value} once. Illegal Value. ".format(value=encoded_value))
except ImportError:
raise ImportError("Base64 import failed")
print(decode('c2FtcGxlcGFzc3dvcmQ='))
I have problem with UnicodeEncodeError in my users_information list:
{u'\u0633\u062a\u064a\u062f#nimbuzz.com': {'UserName': u'\u0633\u062a\u064a\u062f#nimbuzz.com', 'Code': 5, 'Notes': '', 'Active': 0, 'Date': '12/07/2014 14:16', 'Password': '560pL390T', 'Email': u'yuyb0y#gmail.com'}}
And I need to run this code to get users information:
def get_users_info(type, source, parameters):
users_registertion_file = 'static/users_information.txt'
fp = open(users_registertion_file, 'r')
users_information = eval(fp.read())
if parameters:
jid = parameters+"#nimbuzz.com"
if users_information.has_key(jid):
reply(type, source, u"User name:\n" +str(users_information[jid]['UserName'])+ u"\nPassword:\n" +str(users_information[jid]['Password'])+ u"\nREG-code:\nP" +str(users_information[jid]['Code'])+ u"\nDate:\n" +str(users_information[jid]['Date'])+ u"\naccount status:\n " +str(users_information[jid]['Active']))
else:
reply(type, source, u"This user " +parameters+ u" not in user list")
else:
reply(type, source, u"write the id after command")
but when I try to get users information I get this error:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
I try to unicode the jid using unicode('utf8'):
jid = parameters.encode('utf8')+"#nimbuzz.com"
but I get the same error:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
Please how I can solve this problem and as you see the UserName key in the users_information list look like:
u'\u0633\u062a\u064a\u062f#nimbuzz.com'
and the users_information list located in txt file.
You'll not find your user information unless jid is a unicode string. Make sure parameters is a unicode value here, and it'll be easier to use string formatting here:
jid = u"{}#nimbuzz.com".format(parameters)
If you use an encoded bytestring, Python will not find your username in the dictionary as it won't know what encoding you used for the string and won't implicitly decode or encode to make the comparisons.
Next, you cannot call str() on a Unicode value without specifying a codec:
str(users_information[jid]['UserName'])
This is guaranteed to throw an UnicodeEncodeError exception if users_information[jid]['UserName'] contains anything other than ASCII codepoints.
You need to use Unicode values throughout, leave encoding the value to the last possible moment (preferably by leaving it to a library).
You can use string formatting with unicode objects here too:
reply(type, source,
u"User name:\n{0[UserName]}\nPassword:\n{0[Password]}\n"
u"REG-code:\nP{0[Code]}\nDate:\n{0[Date]}\n"
u"account status:\n {0[Active]}".format(users_information[jid]))
This interpolates the various keys from users_information[jid] without calling str on each value.
Note that dict.has_key() has been deprecated; use the in operator to test for a key instead:
if jid in users_information:
Last but not least, don't use eval() if you can avoid it. You should use JSON here for the file format, but if you cannot influence that then at least use ast.literal_eval() on the file contents instead of eval() and limit permissible input to just Python literal syntax:
import ast
# ...
users_information = ast.literal_eval(fp.read())
I had some problem years ago:
jid = parameters+"#nimbuzz.com"
must be
jid = parameters+u"#nimbuzz.com"
and put it at first or second row:
#coding:utf8
Example for Martijn Pieters - on my machine
Python 2.7.8 (default, Jul 1 2014, 17:30:21)
[GCC 4.9.0 20140604 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=u'asdf'
>>> b='ваывап'
>>> a+b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)
>>> c=u'аыиьт'
>>> a+c
u'asdf\u0430\u044b\u0438\u044c\u0442'
>>>
Hi can you help me decode this message and what to do:
main.py", line 1278, in post
message.body = "%s %s/%s/%s" % (msg, host, ad.key().id(), slugify(ad.title.encode('utf-8')))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
Thanks
UPDATE having tried removing the encode call it appears to work:
class Recommend(webapp.RequestHandler):
def post(self, key):
ad= db.get(db.Key(key))
email = self.request.POST['tip_email']
host = os.environ.get("HTTP_HOST", os.environ["SERVER_NAME"])
senderemail = users.get_current_user().email() if users.get_current_user() else 'info#monton.cl' if host.endswith('.cl') else 'info#monton.com.mx' if host.endswith('.mx') else 'info#montao.com.br' if host.endswith('.br') else 'admin#koolbusiness.com'
message = mail.EmailMessage(sender=senderemail, subject="%s recommends %s" % (self.request.POST['tip_name'], ad.title) )
message.to = email
message.body = "%s %s/%s/%s" % (self.request.POST['tip_msg'],host,ad.key().id(),slugify(ad.title))
message.send()
matched_images=ad.matched_images
count = matched_images.count()
if ad.text:
p = re.compile(r'(www[^ ]*|http://[^ ]*)')
text = p.sub(r'\1',ad.text.replace('http://',''))
else:
text = None
self.response.out.write("Message sent<br>")
path = os.path.join(os.path.dirname(__file__), 'market', 'market_ad_detail.html')
self.response.out.write(template.render(path, {'user_url':users.create_logout_url(self.request.uri) if users.get_current_user() else users.create_login_url(self.request.uri),
'user':users.get_current_user(), 'ad.user':ad.user,'count':count, 'ad':ad, 'matched_images': matched_images,}))
The problem here is your underlying model (message.body) only wants ASCII text but you're trying to give it a string encoded in unicode.
But since you've got a normal ascii string here, you can just make python print out the '?' character when you've got a non-ascii-printing string.
"UNICODE STRING".encode('ascii','replace').decode('ascii')
So like from your example above:
message.body = "%s %s/%s/%s" % \
(msgencode('ascii','replace').decode('ascii'),
hostencode('ascii','replace').decode('ascii'),
ad.key().id()encode('ascii','replace').decode('ascii'),
slugify(ad.title)encode('ascii','replace').decode('ascii'))
Or just encode/decode on the variable that has the unicode character.
But this isn't an optimal solution. The best idea is to make message.body a unicode string. Being that doesn't seem feasible (I'm not familiar with GAE), you can use this to at least not have errors.
You've got a Unicode character in a place that you're not supposed to. Most often I find this error is having MS Word-style slanted quotes.
One of these fields has some characters that cannot be encoded. If you switch to python 3 (it has better unicode support), or you change the encoding of the entire script the problem should stop, about the best way to change the encoding in 2.x is using the encoding comment line. If you see http://evanjones.ca/python-utf8.html you will see more of an explanation of using python with utf-8 support the best suggestion is add # -*- coding: utf-8 -*- to the top of your script. And handle scripts like this
s = "hello normal string"
u = unicode( s, "utf-8" )
backToBytes = u.encode( "utf-8" )
I had a similar problem when using Django norel and Google App Engine.
The problem was at the folder containing the application. Probably isn't this the problem described in this question, but, maybe helps someone don't waste time like me.
Try first change you application folder maybe to /home/ and try to run again, if doesn't works, try something more.
I have a text-box which allows users to enter a word.
The user enters: über
In the backend, I get the word like this:
def form_process(request):
word = request.GET.get('the_word')
word = word.encode('utf-8')
#word = word.decode('utf-8')
print word
For some reason, I cannot decode or encode this!!
It gives me the error:
UnicodeEncodeError
('ascii', u'\ufffd', 0, 1, 'ordinal not in range(128)')
Edit: When I do "repr(word)", this is what I get:
u'\ufffd'
Did you remember to put:
accept-charset="utf-8"
in the form tag?
EDIT: Is the DEFAULT_CHARSET in settings.py set to 'utf-8' ?
Solved!
I had escape(word) ...in the javascript ...before I passed it to the server.
Is there any reason to use print word? If not, its should work without those lines.
def form_process(request):
word = request.GET.get('the_word')