gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:45 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com