Python len和str.splitlines()的怪异行为
我正在向库发出HTTP请求,我想创建一个简单的响应对象来解析我的数据。我遇到的问题如下。将response对象传递给我的ApiResponse时,我将response.text拆分为几行,以计数它们,并知道它是有多行还是单行。问题是splitlines列表在对其应用len()时给了我一个错误,但在控制台中它工作正常 这是一个类:Python len和str.splitlines()的怪异行为,python,string,typeerror,python-3.4,Python,String,Typeerror,Python 3.4,我正在向库发出HTTP请求,我想创建一个简单的响应对象来解析我的数据。我遇到的问题如下。将response对象传递给我的ApiResponse时,我将response.text拆分为几行,以计数它们,并知道它是有多行还是单行。问题是splitlines列表在对其应用len()时给了我一个错误,但在控制台中它工作正常 这是一个类: class ApiResponse(object): pattern = re.compile(r'([a-zA-Z]+): ([a-zA-Z0-9]+)') # o
class ApiResponse(object):
pattern = re.compile(r'([a-zA-Z]+): ([a-zA-Z0-9]+)') # original ([a-zA-Z]+)\: ([a-zA-Z0-9]+)
response_type = ResponseType.OK
response_mode = ResponseMode.SINGLE
def __init__(self, r: resp):
self.r = r
self.parse(r)
self.data = None
def parse(self, r: resp):
"""
Method that parses the response from the API
:param r:Response
"""
if r.status_code != 200:
self.response_type = ResponseType.ERR
if len(r.text.splitlines()) > 1:
self.response_mode = ResponseMode.MULTI
for line in r.text.splitlines():
match = self.pattern.search(line)
if match is None:
break
print(match.group(1, 2)) # REMOVE testing
self.response_type = ResponseType[match.group(1)]
>>> import sys
>>> print(sys.version)
3.4.2 (default, Oct 8 2014, 13:08:17)
[GCC 4.9.1]
>>> import requests
>>> from clickapy.response import ApiResponse
>>> r = requests.get(API_URL, {'user': USER, 'password': PASS, 'api_id': API_ID})
>>> api_response = ApiResponse(r)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/eefret/PycharmProjects/clickapy/clickapy/response.py", line 32, in __init__
self.parse(r)
File "/home/eefret/PycharmProjects/clickapy/clickapy/response.py", line 43, in parse
if len(r.text.splitlines()) > 1:
TypeError: object of type 'builtin_function_or_method' has no len()
>>> len(r.text.splitlines())
1
这是控制台输出:
class ApiResponse(object):
pattern = re.compile(r'([a-zA-Z]+): ([a-zA-Z0-9]+)') # original ([a-zA-Z]+)\: ([a-zA-Z0-9]+)
response_type = ResponseType.OK
response_mode = ResponseMode.SINGLE
def __init__(self, r: resp):
self.r = r
self.parse(r)
self.data = None
def parse(self, r: resp):
"""
Method that parses the response from the API
:param r:Response
"""
if r.status_code != 200:
self.response_type = ResponseType.ERR
if len(r.text.splitlines()) > 1:
self.response_mode = ResponseMode.MULTI
for line in r.text.splitlines():
match = self.pattern.search(line)
if match is None:
break
print(match.group(1, 2)) # REMOVE testing
self.response_type = ResponseType[match.group(1)]
>>> import sys
>>> print(sys.version)
3.4.2 (default, Oct 8 2014, 13:08:17)
[GCC 4.9.1]
>>> import requests
>>> from clickapy.response import ApiResponse
>>> r = requests.get(API_URL, {'user': USER, 'password': PASS, 'api_id': API_ID})
>>> api_response = ApiResponse(r)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/eefret/PycharmProjects/clickapy/clickapy/response.py", line 32, in __init__
self.parse(r)
File "/home/eefret/PycharmProjects/clickapy/clickapy/response.py", line 43, in parse
if len(r.text.splitlines()) > 1:
TypeError: object of type 'builtin_function_or_method' has no len()
>>> len(r.text.splitlines())
1
导入系统
>>>打印(系统版本)
3.4.2(默认,2014年10月8日,13:08:17)
[GCC 4.9.1]
>>>导入请求
>>>从单击apy.response导入api响应
>>>r=requests.get(API\uURL,{'user':用户,'password':PASS,'API\uID':API\uID})
>>>api_响应=api响应(r)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/home/eefret/PycharmProjects/clickapy/clickapy/response.py”,第32行,在__
self.parse(r)
文件“/home/eefret/PycharmProjects/clickapy/clickapy/response.py”,解析中第43行
如果len(r.text.splitlines())>1:
TypeError:类型为“内置函数”或“方法”的对象没有len()
>>>len(r.text.splitlines())
1.
为什么会这样?这对我来说毫无意义,我正在执行相同的代码,欢迎提供任何帮助或反馈。我的一位朋友(全部归功于Mariano Garcia)帮助了我,由于他没有SO帐户,我将发布解决此行为的方法,我的控制台正在强制执行utf-8
,但在内部,文本仍然必须进行编码,那么是什么解决了这一问题呢?将if len(r.text.splitlines())>1:
更改为if len(r.text.encode(“utf-8”).splitlines())>1:
完整代码:
class ApiResponse(object):
pattern = re.compile(r'([a-zA-Z]+): ([a-zA-Z0-9]+)') # original ([a-zA-Z]+)\: ([a-zA-Z0-9]+)
response_type = ResponseType.OK
response_mode = ResponseMode.SINGLE
def __init__(self, r: resp):
self.r = r
self.parse(r)
self.data = None
def parse(self, r: resp):
"""
Method that parses the response from the API
:param r:Response
"""
if r.status_code != 200:
self.response_type = ResponseType.ERR
if len(r.text.encode("utf-8").splitlines()) > 1:
self.response_mode = ResponseMode.MULTI
for line in r.text.splitlines():
match = self.pattern.search(line)
if match is None:
break
print(match.group(1, 2)) # REMOVE testing
self.response_type = ResponseType[match.group(1)]
不要将
is
或is not
与数字一起使用!不要将类属性用作预定义的实例属性。不相关,但如果r.status\u code!=200:已更正,但仍在执行相同的操作这是从实际解释器输出粘贴的副本,对吗?因为到目前为止,最可能的解释是代码实际上是len(r.text.splitlines)
。检查response.pyc
文件,如果有,将其删除,然后重新检查response.py
文件,以确保该行的外观符合您的预期。它是否仅发生在需要帐户数据的URL上,或者您是否可以使用类似http://google.com
同样,这样我们就可以自己试试了?