用Python阅读阿拉伯语文件

时间:2015-10-26 22:41:21

标签: python python-3.x

我正在使用阿拉伯语文本文件,这是一个语料库。

我应该怎么做才能在python中导入文件,这样我就可以轻松访问该文件并能够分析它,而不是每次都在解释器中复制和粘贴内容。它是阿拉伯文件,而不是英文。

5 个答案:

答案 0 :(得分:9)

阅读和编写纯文本时最重要的是知道并指定纯文本encoding。你不应该让Python guesses成为你的编码,特别是在现实世界的程序中(编码应该是可配置的,或者你要求用户进行编码)。

许多人都不会遇到英文文本问题,因为ASCII是大多数编码的子集。问题出在那里,一旦程序试图以不同的编码读取或写入文本,它们就会遇到问题。

大多数阿拉伯语文本都是以(按人气 1 Windows-1256UTF-8CP720ISO 8859-6排序的。您应该提前知道 您的纯文本使用的编码,例如,当您保存文件时,大多数文本编辑器都允许您选择编码。

我有三个文件,名称为طارق,但有3种不同的编码。将文件作为原始二进制数据读取,可以显示这些文件的不同之处,尽管它们是相同的文本:

>>> f = open('file-utf8.txt', 'rb')
>>> f.read()
b'\xd8\xb7\xd8\xa7\xd8\xb1\xd9\x82'
>>>
>>> f = open('file-cp720.txt', 'rb')
>>> f.read()
b'\xe1\x9f\xa9\xe7'
>>>
>>> f = open('file-windows1256.txt', 'rb')
>>> f.read()
b'\xd8\xc7\xd1\xde'
>>>

阅读这些文件的正确方法是告诉Python应该使用哪种编码,以便将其解码为internal Unicode representation(使用/Python33/Lib/encodings/中的映射表):

>>> f = open('file-utf8.txt', encoding='utf-8')
>>> f.read()
'طارق'
>>>
>>> f = open('file-cp720.txt', encoding='cp720')
>>> f.read()
'طارق'
>>>
>>> f = open('file-windows1256.txt', encoding='windows-1256')
>>> f.read()
'طارق'
>>>

编码问题不仅与文件有关。每当您从外部源向程序读取文本时,例如文件,控制台,网络套接字,你必须知道编码。此外,当您写入外部源时,您必须将文本编码为正确的编码。

编码必须一致,如果您的控制台使用Latin-1并且您尝试写入控制台,即打印,您将获得一些无意义的单词,或者,如果您幸运,您将获得{{1}例外。

有一些方法可以猜测编码,但我不愿意使用它们,因为它们只能掩盖问题。它迟早会到来。

1 如果由您自己决定,请使用UTF-8,因为它得到了很好的支持。

答案 1 :(得分:0)

将其用于在Python中读取Urdu文件:

File = open("Infixes.txt",encoding = "utf-8")
print(File.read())

答案 2 :(得分:0)

File = open("Infixes.txt",encoding = "utf-16")
print(File.read())

这适用于Windows 8.1和64位处理器

答案 3 :(得分:0)

读取阿拉伯文本文件的正确编码是utf_8和utf_16。但是,您必须同时尝试两者,并查看哪种文件适合您的文件。您可以通过使用编解码器包并设置正确的encoding参数来执行此操作。

import codecs, sys
# pass your file as a command-line argument 
# try "utf-16" encoding if this does not work 



for line in codecs.open(sys.argv[1], encoding = "utf_8"):
    print(line.strip()

答案 4 :(得分:-1)

阿拉伯语通常用Unicode表示。

通常,您可以读取该文件,然后转换为Unicode:

<div id="login-menu">
        <div id="login-bubble">
            <form method="post" action="/ftp/index.php?login=1">
                <span id="login-username">
                    <input type="text" name="username" id="txt_username" placeholder="username" required="" value="" />
                    <span class="username-icon"><i class="fa fa-user"></i></span>
                </span>
                <span id="login-password">
                    <input type="password" name="password" id="txt_password" placeholder="password" required="" />
                    <span class="password-icon"><i class="fa fa-lock"></i></span>
                </span>
                <span id="forgotten-pwd">
                    <a href=""><span>Forgotten Password ?</span></a>
                </span>
                <span id="submit-button">
                    <input type="hidden" name="redirect" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
                    <button type="submit" name="submit" id="sub-login"><i id="submit"class="fa fa-long-arrow-right"></i></button>
                </span>
                <span id="button-border"></span>
            </form>
        </div>
    </div>

有关详细信息,请参阅https://docs.python.org/2/howto/unicode.html#reading-and-writing-unicode-data