Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show the right word in my code, my code is : os.urandom(64)

Tags:

python

My code is:

print os.urandom(64)

which outputs:

> "D:\Python25\pythonw.exe"  "D:\zjm_code\a.py" 
\xd0\xc8=<\xdbD'
\xdf\xf0\xb3>\xfc\xf2\x99\x93
=S\xb2\xcd'\xdbD\x8d\xd0\\xbc{&YkD[\xdd\x8b\xbd\x82\x9e\xad\xd5\x90\x90\xdcD9\xbf9.\xeb\x9b>\xef#n\x84

which isn't readable, so I tried this:

print os.urandom(64).decode("utf-8")

but then I get:

> "D:\Python25\pythonw.exe"  "D:\zjm_code\a.py" 
Traceback (most recent call last):
  File "D:\zjm_code\a.py", line 17, in <module>
    print os.urandom(64).decode("utf-8")
  File "D:\Python25\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-3: invalid data

What should I do to get human-readable output?

like image 317
zjm1126 Avatar asked Jan 28 '26 16:01

zjm1126


1 Answers

No shortage of choices. Here's a couple:

>>> os.urandom(64).encode('hex')
'0bf760072ea10140d57261d2cd16bf7af1747e964c2e117700bd84b7acee331ee39fae5cff6f3f3fc3ee3f9501c9fa38ecda4385d40f10faeb75eb3a8f557909'
>>> os.urandom(64).encode('base64')
'ZuYDN1BiB0ln73+9P8eoQ3qn3Q74QzCXSViu8lqueKAOUYchMXYgmz6WDmgJm1DyTX598zE2lClX\n4iEXXYZfRA==\n'
like image 131
Ignacio Vazquez-Abrams Avatar answered Jan 31 '26 06:01

Ignacio Vazquez-Abrams