Python 2.7 如何将re.findall()响应中的ip地址数组转换为字符串

Python 2.7 如何将re.findall()响应中的ip地址数组转换为字符串,python-2.7,Python 2.7,我有这段代码,希望最终将re.findall()响应中的IP地址转换为字符串。但我总是会出错(见下文) 我总是犯这个错误 TypeError: not all arguments converted during string formatting 有人能告诉我什么是最好的转换方法吗? 谢谢。我想您的问题是,您正在将re.findall()转换为字符串,但该函数返回注释中提到的列表。因此,最好使用re.findall(…)[0]如果您只需要一个结果/一个ip,如果不需要,则打印所有结果: ip

我有这段代码,希望最终将
re.findall()
响应中的IP地址转换为字符串。但我总是会出错(见下文)

我总是犯这个错误

TypeError: not all arguments converted during string formatting
有人能告诉我什么是最好的转换方法吗?
谢谢。

我想您的问题是,您正在将
re.findall()
转换为字符串,但该函数返回注释中提到的列表。因此,最好使用
re.findall(…)[0]
如果您只需要一个结果/一个ip,如果不需要,则打印所有结果:

ips = re.findall(r'[0-9]+(?:\.[0-9]+){3}', request)
for ip in ips:
    print str(ip)
就我个人而言,我认为最好是打印所有内容,或者至少检查
re.findall()
的长度,因为这样很容易遗漏内容。此外,我还想向您推荐用于正则表达式调试的站点。:)


使用适当的API返回结构化结果,而不是任意文本,并且不用担心它?例如,
print ip
工作正常(它打印
['a.b.c.d']
,因为您已经将列表转换为字符串,但这是预期的输出),您能提供吗?
ip=re.findall(r'[0-9]+(?:\[0-9]+){3},请求)[0]
工作吗
re.findall()
ips = re.findall(r'[0-9]+(?:\.[0-9]+){3}', request)
for ip in ips:
    print str(ip)