[Tutor] Convert os.random output to a string

Jay Lozier jslozier at gmail.com
Fri Mar 7 00:32:15 CET 2014


Hi,

My first question

I am try to generate random password strings of an arbitrary user 
selected length. I read to generate a random string of a user supplied 
length I can use os.urandom(n). I want to convert the output to a human 
readable string. I would like to store the string for later processing.

Also, I would like to limit the characters to the US keyboard, so I 
might be using the wrong function.

The following is the output from my terminal - Python 3.3.4 on Manjaro 
Linux shows the generation of a random :

Python 3.3.4 (default, Feb 11 2014, 15:56:08)
[GCC 4.8.2 20140206 (prerelease)] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> from os import urandom
 >>> a = urandom(16)
 >>> type(a)
<class 'bytes'>
 >>> a_str = a.decode()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 0: 
invalid start byte
 >>>

I am probably missing something very obvious.

Thanks in advance!

-- 
Jay Lozier
jslozier at gmail.com



More information about the Tutor mailing list