[英]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
. 让我们假设你有一个字符串s
在编码encoding
。 To get the same string in UTF-8
, you can use 要在UTF-8
获得相同的字符串,可以使用
s.decode(encoding).encode("utf-8")
If you have an ascii encoded file,f: 如果您有一个ascii编码文件,请执行以下操作:
1)f1=unicode(f) 2)f2=f1.encode('utf-8') 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)" 这样,您就可以避免出现以下错误:“ UnicodeEncodeError:'ascii'编解码器无法在位置5编码字符u'\\ xdf':序数不在range(128)中“
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.