使用arabicreshaperin和bidi转换py格式的文本(从文件转换)

2024-10-03 21:35:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在python中使用Reformerin和bidi来转换一些阿拉伯语文本

我的问题是如何使用阿拉伯语重塑器转换txt文件中的文本,并将转换后的文本保存到其他txt文件中

这是我转换一行的代码

import arabic_reshaper
from bidi.algorithm import get_display
text = 'سلام محمد خوبی'

test = arabic_reshaper.reshape(text)
print(get_display(test))

我知道这是一个简单的问题,但直到今天我才使用Python tnx


Tags: 文件代码textfromtest文本importtxt
1条回答
网友
1楼 · 发布于 2024-10-03 21:35:30

可能使用列表

import arabic_reshaper
from bidi.algorithm import get_display
text = 'سلام محمد خوبی'
test = arabic_reshaper.reshape(text)
print(get_display(test))
lines = []

with open("file.txt", encoding="utf8") as file_in:
    for line in file_in:
        lines.append(arabic_reshaper.reshape(line))

#Save it to file with a line break
with open("file2.txt", "w", encoding="utf8") as output: #created if it doesnt exist
    for line in lines:
        output.write(str(line) + '\n')

相关问题 更多 >