简体   繁体   English

在python 3中编码字符串“ tamilnadu”

[英]Encode the string “tamilnadu” in python 3

I want to encode the string "tamilnadu" and the output is like "dGFtaWxuYWR1" 我想编码字符串“ tamilnadu”,输出类似于“ dGFtaWxuYWR1”

str = "tamilnadu";
print str.encode('base64','strict') //dGFtaWxuYWR1

But when I am encode the string its shows an error like this 但是当我编码字符串时,它显示了这样的错误

'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs

I tired all the encoding techniques, but the output for the sting is not like this encode value "dGFtaWxuYWR1" 我已厌倦了所有编码技术,但字符串的输出却不像此编码值“ dGFtaWxuYWR1”

How can I encode the string in python 3.4 如何在python 3.4中编码字符串

You need base64 module 您需要base64模块

import base64
base64.b64encode(bytes('tamilnadu', 'utf-8'))

Also, don't use str as a variable name - its a keyword in python which represents string type. 另外,不要将str用作变量名-它是python中代表字符串类型的关键字。

Use base64 library 使用base64库

    import base64
    c="tamilnadu"
    c=base64.b64encode(bytes(c, 'utf-8')) // b'dGFtaWxuYWR1'
    c1=base64.b64decode(c) 
    c=str(c).replace("'","")  // bdGFtaWxuYWR1
    print("Encode",c[1:])       // dGFtaWxuYWR1 
    c1=str(c1).replace("'","") // b'tamilnadu'
    print("Decode=",c1[:1]) // tamilnadu

After encode you have replace the ' and first character b 编码后,您已替换了'和第一个字符b

You need base64 module 您需要base64模块

import base64
base64.b64encode(bytes('tamilnadu', 'utf-8'))

Also, don't use str as a variable name - its a keyword in python which represents string type. 另外,不要将str用作变量名-它是python中代表字符串类型的关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM