Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Python不能识别阿拉伯语字符?_Python_String_List_Printing_Arabic - Fatal编程技术网

为什么Python不能识别阿拉伯语字符?

为什么Python不能识别阿拉伯语字符?,python,string,list,printing,arabic,Python,String,List,Printing,Arabic,我有一个简单的代码。当我使用英语字符时,它工作得很好,但当我使用阿拉伯语字符时,它似乎无法识别它。 当我输入“hello”时,它返回英语 但当我输入“سلام”时,什么都没发生 x = input("please enter something: ") if x == "سلام" : print("Arabic") elif x == "hello" : print("English") Python的哪个版本?如果这些是Unicode字符,Python 3应该能够很好地处理

我有一个简单的代码。当我使用英语字符时,它工作得很好,但当我使用阿拉伯语字符时,它似乎无法识别它。 当我输入“hello”时,它返回英语 但当我输入“سلام”时,什么都没发生

x = input("please enter something: ")


if x == "سلام" :
  print("Arabic")

elif x == "hello" :

  print("English")

Python的哪个版本?如果这些是Unicode字符,Python 3应该能够很好地处理它们,但是在Python 2中,您必须指定一种编码,您使用的Python版本是什么?在Python2.7中,您应该在python文件的第一行中键入编码声明:
#-*-coding:utf-8-*-

在Python2.7中运行此代码会导致错误消息
SyntaxError:file…
中的非ASCII字符'\xd8'

在第一行添加
#-*-coding:utf-8-*-
后,我得到输出:
阿拉伯语

在Python3.6.5中,我得到了输出
阿拉伯语
,而没有在第一行添加编码声明

试试这个:

# -*- coding: utf-8 -*-

x = input("please enter something: ")


if x == "سلام" :
    print("Arabic")

elif x == "hello" :
    print("English")

Python编译器不能直接解析UTF-8字符,任何拉丁符号等都不能直接由该编译器处理

from googletrans import Translator
translator = Translator()
x = translator.translate(input("please enter something: "))
if x.text == "سلام":
    print("Arabic")

elif x.text == "hello":
    print("English")

elif x.text == "Hello":
    print("Translated")
解决方案-请使用翻译程序解析UTF-8并使编译器能够理解

from googletrans import Translator
translator = Translator()
x = translator.translate(input("please enter something: "))
if x.text == "سلام":
    print("Arabic")

elif x.text == "hello":
    print("English")

elif x.text == "Hello":
    print("Translated")

在我的文本之前,我在代码和字母“u”中使用了
编码:utf-8
,它工作得非常好

x = input("please enter something: ")
if x == u'سلام' :
  print("Arabic")
elif x == "hello" :
  print("English")

如果您
打印(repr(x),x)
,您会得到什么?您使用的是哪个操作系统?Windows具有utf-8的浇注支持。只有最新版本才可以。