日语中的python函数 [英] Japanese in python function

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

问题描述

我用Python编写了一个函数,用于告诉我两个词是否相似.

I wrote a function in Python which is used to tell me whether the two words are similar or not.

现在,我想在相同的函数中传递日语文本.它给出错误不是ASCII字符".我尝试使用utf-8编码,但随后却给出了相同的错误

Now I want to pass Japanese text in my same function. It is giving error "not a ascii character." I tried using utf-8 encoding, but then it giving the same error

Non-ASCII character '\xe3' in file

有没有办法做到这一点?我不能为此生成msg文件,因为2关键字将不是恒定的.

Is there any way to do that? I cant generate the msg file for that since the 2 keyword will be not be constant.

代码在这里

def filterKeyword(keyword, adText, filterType):
if (filterType == 'contains'):
    try :
        adtext = str.lower(adText)
        keyword = str.lower(keyword)
        if (adtext.find(keyword)!=-1):
            return '0'
    except:
        return '1'
if (filterType == 'exact'):
    var = cmp(str.lower(adText), str.lower(keyword))
    if(var == 0 ):
        return '0'

return '1'

我使用了以下内容:

filterKeyword(unicode('ポケモン').encode("utf-8"), unicode('黄色のポケモン').encode("utf-8"), 'contains')

filterKeyword('ポケモン'.encode("utf-8"), '黄色のポケモン'.encode("utf-8"), 'contains')

他们两个都给出了错误.

Both of them are giving the error.

推荐答案

这对我有用:

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

def filterKeyword(keyword, adText, filterType):
    # same as yours

filterKeyword(u'ポケモン', u'黄色のポケモン', 'contains')

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

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