python - 输入文件的字符串转换

标签 python utf8-decode

我是 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/

相关文章:

python - 如何在卸载中转义单引号

python - 扭曲在 window 下

java - 将编码的 UTF-8 字符转换为 android 中的特殊重音字符

python - 在 Python 中使用 GStreamer 从网络摄像头抓取随机帧

python - 用Python重新挖掘信息

c# - FileUpload 服务器控件和 unicode 字符

javascript - 如何使用 jQuery 解码 UTF8 字符?

C#、UTF-8 和编码字符

python - 根据 Pandas 列表列中的条件创建新列