Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python re.findall打印列表而不是字符串_Python_Regex_Urllib2 - Fatal编程技术网

Python re.findall打印列表而不是字符串

Python re.findall打印列表而不是字符串,python,regex,urllib2,Python,Regex,Urllib2,如果运行脚本,输出将类似于以下内容: address = ('http://www.somesite.com/article.php?page=' +numb) html = urllib2.urlopen(address).read() regex = re.findall(r"([a-f\d]{12})", html) 如何使脚本打印此输出(请注意换行符): 有什么帮助吗?re.findall()返回一个列表。因此,您可以迭代列表并单独打印每个元素,如下所示: aaaaaaaaaaaa b

如果运行脚本,输出将类似于以下内容:

address = ('http://www.somesite.com/article.php?page=' +numb)
html = urllib2.urlopen(address).read()
regex = re.findall(r"([a-f\d]{12})", html)
如何使脚本打印此输出(请注意换行符):

有什么帮助吗?

re.findall()
返回一个列表。因此,您可以迭代列表并单独打印每个元素,如下所示:

aaaaaaaaaaaa
bbbbbbbbbbbb
cccccccccccc
或者您可以按照@bigOTHER的建议将列表合并成一个长字符串并打印该字符串。它基本上在做同样的事情

资料来源:

re.findall(pattern,string,flags=0)返回所有不重叠的 字符串中模式的匹配,如字符串列表。字符串是 从左到右扫描,并按找到的顺序返回匹配项。如果 如果模式中存在一个或多个组,则返回 组;如果模式有多个元组,这将是一个元组列表 小组。空匹配项包括在结果中,除非它们与 另一场比赛的开始


只需像这样打印
regex

address = ('http://www.somesite.com/article.php?page=' +numb)
html = urllib2.urlopen(address).read()
for match in re.findall(r"([a-f\d]{12})", html)
    print match


对结果使用
join

address = ('http://www.somesite.com/article.php?page=' +numb)
html = urllib2.urlopen(address).read()
regex = re.findall(r"([a-f\d]{12})", html)
print "\n".join(regex)

我的代码已经在“for”语句中,是否有其他解决方案?不过谢谢。如果您询问的是输出,您应该包括输出代码。它应该返回一个
列表
。如果您希望它每行打印一项,这是一项非常基本的任务,您可以在中找到。
print "\n".join(regex)
address = ('http://www.somesite.com/article.php?page=' +numb)
html = urllib2.urlopen(address).read()
regex = re.findall(r"([a-f\d]{12})", html)
print "\n".join(regex)
"".join("{0}\n".format(x) for x in re.findall(r"([a-f\d]{12})", html)