首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用bidi进行双向model.txt

使用bidi进行双向model.txt
EN

Stack Overflow用户
提问于 2019-04-20 20:37:26
回答 1查看 37关注 0票数 0

我想在我的代码中使用model.txt行,但行中的所有单词都是波斯语(从右到左).i使用此代码更正它们,但它给我错误。我知道如何解决错误,但如果我把我的线改为字符串,我不能纠正他们的形状和direction.any帮助?

代码语言:javascript
复制
import arabic_reshaper

from bidi.algorithm import get_display


def readFile():
    with open('D:/visual stadio/python/captcha maker/test/model.txt','r') as file:
        lines= file.readlines()

    reshaped_text = arabic_reshaper.reshape(lines) 
       #if i use reshaped_text = arabic_reshaper.reshape(str(lines)) it will 
       #work fine but it will give me this answer: ['سلام\n', 
       #'سلامس\n', 'آدامس\n', 'پنیر\n', 'چتر\n','پاوه'] this are my words in model.txt but not fixed.
    bidi_text = get_display(reshaped_text)         

    return bidi_text 
bidi_text=readFile()
print(bidi_text)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-22 20:01:45

解决了!我为我的文件使用了一个编码,它是固定的!感谢我的朋友armin告诉我我的错误。

代码语言:javascript
复制
import arabic_reshaper
import codecs
from bidi.algorithm import get_display


def readFile():
    with open('D:/visual stadio/python/captcha maker/test/model.txt','r',encoding='utf8') as file:
        lines= file.readlines()
        lista=[]

        for line in lines:
                reshaped_text = arabic_reshaper.reshape(line) 
                bidi_text = get_display(reshaped_text)         
                lista.append(bidi_text)
        return lista
lista=readFile()
print(lista)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55773582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档