我向postag该following text的API发出一些请求,如下所示:
def pos(text):
payload = {'key': 'thekey', 'of': 'json', 'ilang': 'ES', \
'txt': text, \
'tt': 'a', \
'uw': 'y', 'lang': 'es'}
r = requests.get('http://api.meaningcloud.com/parser-2.0', params=payload, stream = True)
return r.json()
一开始,它给了我一个ValueError
:
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
<ipython-input-19-ac09c6405340> in <module>()
1
----> 2 df['tags'] = df['tweets'].apply(transform)
3 df
/usr/local/lib/python3.5/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
2292 else:
2293 values = self.asobject
-> 2294 mapped = lib.map_infer(values, f, convert=convert_dtype)
2295
2296 if len(mapped) and isinstance(mapped[0], Series):
pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:66124)()
<ipython-input-18-707ac7b399b4> in transform(a_lis)
25
26 def transform(a_lis):
---> 27 analysis = pos(str(a_lis))
28 a_list = parse_tree(analysis['token_list'], [])
29 return a_list
<ipython-input-18-707ac7b399b4> in pos(text)
8
9 r = requests.get('http://api.meaningcloud.com/parser-2.0', params=payload, stream = True)
---> 10 return r.json()
11
12 def parse_tree(token, a_list):
/usr/local/lib/python3.5/site-packages/requests/models.py in json(self, **kwargs)
864 # used.
865 pass
--> 866 return complexjson.loads(self.text, **kwargs)
867
868 @property
/usr/local/lib/python3.5/site-packages/simplejson/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, **kw)
514 parse_constant is None and object_pairs_hook is None
515 and not use_decimal and not kw):
--> 516 return _default_decoder.decode(s)
517 if cls is None:
518 cls = JSONDecoder
/usr/local/lib/python3.5/site-packages/simplejson/decoder.py in decode(self, s, _w, _PY3)
368 if _PY3 and isinstance(s, binary_type):
369 s = s.decode(self.encoding)
--> 370 obj, end = self.raw_decode(s)
371 end = _w(s, end).end()
372 if end != len(s):
/usr/local/lib/python3.5/site-packages/simplejson/decoder.py in raw_decode(self, s, idx, _w, _PY3)
398 elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399 idx += 3
--> 400 return self.scan_once(s, idx=_w(s, idx).end())
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
因此,我处理了异常,并将其应用于具有以下内容的数据帧列:
df = pd.read_csv('../data.csv')
df['tagged_text'] = df['tweets'].apply(transform)
但是,对于一些实例(列),我得到了None
:
text tagged_text
Siento que estoy en un cuarto oscuro y hay sil... [(sentar, VI-S1PSABL-N4), (que, CSSN9), (estar...
Los mejores de @UEoficial Sebastián Jaime, Sey... None
#ColoColoJuegaEnEl13 la primera y adentro mier... None
Juguito heladoooo de melón: me siento se... None
@sxfiacrespo @lunasoledadhern Hola Luna... [(@sxfiacrespo @lunasoledadhern, NPUU-N-), (ho...
因此,我的问题是,为什么在某些文本(列)中我得到None
以及如何正确标记这些None
实例?。注意,我做了一些测试,文本没有问题,因为对于那些None
,返回了一个包含所有标记内容的json。对于example consider this function application。你知道吗
这几乎不起任何作用:
只引用
np.nan
对象的。如果要返回它,需要显式地执行以下操作:否则函数只是。。结束,这意味着返回
None
。你知道吗其他注意事项:
是一种非常昂贵的拼写方法
将JSON加载到Python中,然后再次生成JSON,然后使用YAML解析器将JSON转换回Python有点过分。我还删除了
stream=True
;只有当您希望将响应数据作为流处理时才需要它(而response.json()
方法不需要这样做)。你知道吗根据API documentation,
txt
应该是一个单独的字符串,我不会用str(a_lis)
来产生这个字符串;如果你有一个字符串列表,就用' '.join(a_lis)
把它们连接成一个长字符串。但是,我确信pandas.Series.apply()
将单个值(例如字符串)传递给函数,此时根本不需要连接任何内容(但在这种情况下,a_lis
变量名非常混乱)。你知道吗API还指定它使用POST请求(我很惊讶它们接受GET still)。使用
POST
请求(requests.post()
)将允许您发送更大的文本片段进行分析。使用data
关键字。我在上面的最后一个示例中使用了正确的语法。你知道吗使用GET也是获得
ValueError
的原因:相关问题 更多 >
编程相关推荐