gpt4 book ai didi

python - 为什么 Python 无法识别阿拉伯字符?

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:10 25 4
gpt4 key购买 nike

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

x = input("please enter something: ")


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

elif x == "hello" :

print("English")

最佳答案

您使用的是哪个 Python 版本?在 Python2.7 中,您应该在 python 文件的第一行键入编码声明:# -*- coding: utf-8 -*-

在 Python2.7 中运行此代码会导致错误消息 SyntaxError: Non-ASCII character '\xd8' in file...

# -*- coding: utf-8 -*- 添加到第一行后,我得到输出:Arabic

在 Python3.6.5 中,我得到了输出 Arabic,但没有在第一行添加编码声明。

试试这个:

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

x = input("please enter something: ")


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

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

关于python - 为什么 Python 无法识别阿拉伯字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57134274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com