简体   繁体   中英

how to print the value of an unicode string contained in a variable

I'm using python 2.7 and want to print the value of a field I receive on my server through a form.

I type André for the field name .

name = request.form['stripeBillingName']

How do I print the value of the variable name in a readable encoding? I want to print André and not Andr\\xe9

In source header you can declare

#!/usr/bin/env python
# -*- coding: utf-8 -*-
....

After that you can use utf-8, which should give you your desirable format

name = request.form['stripeBillingName']
nameDec = name.decode('utf8')
print nameDec

You can also encode it however you want eg:

nameEnc = nameDec.encode('cp1250')

Use .decode("utf8")

Ex: "someUnicodeString".decode("utf8")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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