我是 python 的新手,我需要一些帮助来处理这段代码:
这段代码工作正常,它根据我的需要转换字符串。
# -*- coding: utf-8 -*-
import sys
import arabic_reshaper
from bidi.algorithm import get_display
reshaped_text = arabic_reshaper.reshape(u' الحركات')
bidi_text = get_display(reshaped_text)
print >>open('out', 'w'), reshaped_text.encode('utf-8') # This is ok
当我尝试从文件中读取字符串时出现以下错误:
# -*- coding: utf-8 -*-
import sys
import arabic_reshaper
from bidi.algorithm import get_display
with open ("/home/nemo/Downloads/mpcabd-python-arabic-reshaper-552f3f4/data.txt" , "r") as myfile:
data=myfile.read().replace('\n', '')
reshaped_text = arabic_reshaper.reshape(data)
bidi_text = get_display(reshaped_text)
print >>open('out', 'w'), reshaped_text.encode('utf-8')
UnicodeDecodeError:“ascii”编解码器无法解码位置 0 中的字节 0xd8:序号不在范围内 (128)。
任意手
谢谢
最佳答案
The method decode() decodes the string using the codec registered for encoding. It defaults to the default string encoding.
读取utf-8编码文件时,需要使用string.decode('utf8')
写:
data = 'my data'
with open("file.txt" , "w") as f:
f.write(data.encode('utf-8'))
阅读:
with open("file.txt" , "r") as f:
data = f.read().decode('utf-8')
关于python - 输入文件的字符串转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21349727/