python中的阿拉伯语,Unicode和文件 [英] Arabic, Unicode and files in python

查看:242
本文介绍了python中的阿拉伯语,Unicode和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从YouTube上获取一些阿拉伯语文字,然后将其写入文件中并再次阅读.

I am trying to grab some text written in Arabic from Youtube, writting it into a file and reading it again.

获取文本的源文件有:

#!/usr/bin/python
#encoding: utf-8

在文件的开头.

编写文本的步骤如下:

f.write(comment + '\n' )

文件内容是可读的阿拉伯语,因此我认为前面的步骤是正确的.

The file contents is readable Arabic, so I assume the previous steps were correct.

但是当尝试从文件中读取内容(例如将它们写入另一个文件)时,出现了问题:

But the problem appears when trying to read the contents from the file (and writing them for example into another file) like this:

in = open('data_Pass1/EG', 'rb')
out.write(in.read())

这样会导致输出文件如下:

Which results in output file like this:

\xd8\xa7\xd9\x8a\xd9\x87

是什么原因造成的?

推荐答案

在python 3.x中

In python 3.x



in = open('data_Pass1/EG', 'r', encoding='utf-8')
out = open('_file_name_', 'w', encoding='utf-8')

在python 2.x中.

In python 2.x.



import codecs
in = codecs.open('data_Pass1/EG', 'r', encoding='utf-8')
out = codecs.open('_file_name_', 'w', encoding='utf-8')

这篇关于python中的阿拉伯语,Unicode和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆