gpt4 book ai didi

python - "expected string or buffer"

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:27 30 4
gpt4 key购买 nike

我有这个代码:

x=os.system("host www.google.com")
b=re.findall(r'\w',x)
print b

但这会返回以下错误:

TypeError: expected string or buffer  

最佳答案

os.system 的返回值是进程的退出代码。这是一个整数,而不是一个字符串,所以你基本上是这样做的:

>>> re.findall(r'\w', 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 177, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

我认为您要查找的函数是 subprocess.check_output :

>>> import subprocess
>>> print subprocess.check_output(['host', 'www.google.com'])
www.google.com has address 173.194.75.147
www.google.com has address 173.194.75.99
www.google.com has address 173.194.75.103
www.google.com has address 173.194.75.104
www.google.com has address 173.194.75.105
www.google.com has address 173.194.75.106
www.google.com has IPv6 address 2607:f8b0:400c:c03::6a

关于python - "expected string or buffer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12621954/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com