简体   繁体   中英

how to convert string from known encoding to utf-8 on the fly in python?

I know about codecs library, but I don't want to write string to file.

Is there a way to hold resulting string in variable?

Let's assume you have a string s encode in encoding . To get the same string in UTF-8 , you can use

s.decode(encoding).encode("utf-8")

If you have an ascii encoded file,f:

1)f1=unicode(f) 2)f2=f1.encode('utf-8')

I this way, you get rid of errors like:"UnicodeEncodeError: 'ascii' codec can't encode character u'\\xdf' in position 5: ordinal not in range(128)"

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