用百度的自然语言分析Python的,调用的时候报错怎么解决
大智慧社区 发布于2017-04 浏览:3546 回复:9
0
收藏

# -*- coding:gb2312 -*-


# 引入NLP SDK

from aip import AipNlp

from PIL import Image

    

# 定义常量

APP_ID = '你的 App ID'

API_KEY = '你的 API Key'

SECRET_KEY = '你的 Secret Key'

    

# 初始化AipNlp对象

aipNlp = AipNlp(APP_ID, API_KEY, SECRET_KEY)

收藏
点赞
0
个赞
共9条回复 最后由铃戌动质葡回复于2021-01
#10用户已被禁言回复于2021-01

http://xueli2.wikidot.com
http://xueli2.wikidot.com/1
http://xueli2.wikidot.com/2
http://xueli2.wikidot.com/3
http://xueli2.wikidot.com/4
http://xueli2.wikidot.com/5
http://xueli2.wikidot.com/6
http://xueli2.wikidot.com/7
http://xueli2.wikidot.com/8
http://xueli2.wikidot.com/9
http://xueli2.wikidot.com/10
http://xueli2.wikidot.com/11
http://xueli2.wikidot.com/12
http://xueli2.wikidot.com/13
http://xueli2.wikidot.com/14
http://xueli2.wikidot.com/15
http://xueli2.wikidot.com/16
http://xueli2.wikidot.com/17
http://xueli2.wikidot.com/18
http://xueli2.wikidot.com/19
http://xueli2.wikidot.com/20
http://xueli2.wikidot.com/21

0
#9Mudy0908回复于2017-08
#8 harriet_yang回复
还没有, 有没有管理员来看看啊。

我已经换成python2了,在3里就有那个问题

0
#8harriet_yang回复于2017-07
#6 zengqiang47回复
同问,不知道你解决了吗?

还没有, 有没有管理员来看看啊。

0
#7ubirdy回复于2017-07
#5 harriet_yang回复
我的错误能不能帮忙看看: APPID:9759742 from aip import AipNlp # 定义常量 APP_ID = '9759742' API_KEY = 'xxx' SECRET_KEY = 'xxx' # 初始化AipNlp对象 aipNlp = AipNlp(APP_ID, API_KEY, SECRET_KEY) result = aipNlp.wordEmbedding('百度') 运行错误:  Traceback (most recent call last):   File "C:/Users/hyang13/Desktop/superBot/test.py", line 10, in     result = aipNlp.wordEmbedding('百度')   File "C:\Anaconda3\lib\site-packages\aip\nlp.py", line 105, in wordEmbedding     return self._request(self.__wordEmbeddingUrl, self.__processData(data))   File "C:\Anaconda3\lib\site-packages\aip\nlp.py", line 55, in __processData     return json.dumps(content)   File "C:\Anaconda3\lib\json\__init__.py", line 230, in dumps     return _default_encoder.encode(obj)   File "C:\Anaconda3\lib\json\encoder.py", line 198, in encode     chunks = self.iterencode(o, _one_shot=True)   File "C:\Anaconda3\lib\json\encoder.py", line 256, in iterencode     return _iterencode(o, 0)   File "C:\Anaconda3\lib\json\encoder.py", line 179, in default     raise TypeError(repr(o) + " is not JSON serializable") TypeError: b'\xb0\xd9\xb6\xc8' is not JSON serializable Process finished with exit code 1
展开

原因是百度的nlp.py中有这样一个函数在python3中不用再编码,直接return s即可

    def __encode(self, s):

        """

            编码

        """


        if sys.version_info.major == 2:

            if not isinstance(s, unicode):

                s = s.decode('utf8')


        return s.encode('gbk')

把最后一句 return s.encode('gbk')改成 return s


0
#6zengqiang47回复于2017-06
#5 harriet_yang回复
我的错误能不能帮忙看看: APPID:9759742 from aip import AipNlp # 定义常量 APP_ID = '9759742' API_KEY = 'xxx' SECRET_KEY = 'xxx' # 初始化AipNlp对象 aipNlp = AipNlp(APP_ID, API_KEY, SECRET_KEY) result = aipNlp.wordEmbedding('百度') 运行错误:  Traceback (most recent call last):   File "C:/Users/hyang13/Desktop/superBot/test.py", line 10, in     result = aipNlp.wordEmbedding('百度')   File "C:\Anaconda3\lib\site-packages\aip\nlp.py", line 105, in wordEmbedding     return self._request(self.__wordEmbeddingUrl, self.__processData(data))   File "C:\Anaconda3\lib\site-packages\aip\nlp.py", line 55, in __processData     return json.dumps(content)   File "C:\Anaconda3\lib\json\__init__.py", line 230, in dumps     return _default_encoder.encode(obj)   File "C:\Anaconda3\lib\json\encoder.py", line 198, in encode     chunks = self.iterencode(o, _one_shot=True)   File "C:\Anaconda3\lib\json\encoder.py", line 256, in iterencode     return _iterencode(o, 0)   File "C:\Anaconda3\lib\json\encoder.py", line 179, in default     raise TypeError(repr(o) + " is not JSON serializable") TypeError: b'\xb0\xd9\xb6\xc8' is not JSON serializable Process finished with exit code 1
展开

同问,不知道你解决了吗?

0
#5harriet_yang回复于2017-06
#3 周俊316回复
您好,我们的技术正在查看,也请您确认Python 的 PIL 的库是否正确安装[图片]
展开

我的错误能不能帮忙看看:


APPID:9759742

from aip import AipNlp


# 定义常量
APP_ID = '9759742'
API_KEY = 'xxx'
SECRET_KEY = 'xxx'

# 初始化AipNlp对象
aipNlp = AipNlp(APP_ID, API_KEY, SECRET_KEY)
result = aipNlp.wordEmbedding('百度')


运行错误: 


Traceback (most recent call last):

  File "C:/Users/hyang13/Desktop/superBot/test.py", line 10, in

    result = aipNlp.wordEmbedding('百度')

  File "C:\Anaconda3\lib\site-packages\aip\nlp.py", line 105, in wordEmbedding

    return self._request(self.__wordEmbeddingUrl, self.__processData(data))

  File "C:\Anaconda3\lib\site-packages\aip\nlp.py", line 55, in __processData

    return json.dumps(content)

  File "C:\Anaconda3\lib\json\__init__.py", line 230, in dumps

    return _default_encoder.encode(obj)

  File "C:\Anaconda3\lib\json\encoder.py", line 198, in encode

    chunks = self.iterencode(o, _one_shot=True)

  File "C:\Anaconda3\lib\json\encoder.py", line 256, in iterencode

    return _iterencode(o, 0)

  File "C:\Anaconda3\lib\json\encoder.py", line 179, in default

    raise TypeError(repr(o) + " is not JSON serializable")

TypeError: b'\xb0\xd9\xb6\xc8' is not JSON serializable


Process finished with exit code 1



0
#4小宇宙雷雨回复于2017-06

尊敬的大V,您好,我们诚邀您参与微软云时代开发者调研活动。您的反馈对于微软至关重要,可以帮助我们为开发者社区提供更多


有针对性的内容和云资源支持。为了感谢您的参与,我们将在完成调研的用户中抽取幸运者送出包括价值5000元的Azure云服务账户


充值卡、蓝牙耳机等在内的丰富礼品。 调研链接:

https://sojump.com/jq/14366726.aspx?udsid=847273







0
#3周俊316回复于2017-05

您好,我们的技术正在查看,也请您确认Python 的 PIL 的库是否正确安装

0
#2chenxiaoyu3回复于2017-04

请确认Python 的 PIL 的库是否正确安装

0
TOP
切换版块