2017-05-18 40 views
0

我得到的main_watch_images列表中的两个项目,只是要打印的第一个。我试图在findall行之后添加[0],但是它会垂直打印每个字母。我想这是因为for循环在新行上打印所有内容,但是我无法设法将它全部打印在一行上。Python 2.7版 - 让for循环打印无新线

# Extract the first image (this is always the main image) that is in the 
# "LIMITED" category 
main_watch_images = findall('<img.* src="([^"]+).*LIMITED"', dennisov_html) 
# Convert the URL to a full address 
for images in main_watch_images: 
    if images.startswith('/'): 
     images = 'https://denissov.ru' + images 
    print images 

注意这不是和其他问题相同的问题。我是一个绝对的初学者,无法理解其他答案中使用的技巧和操作符等。我需要一个具体的答案来解决我的问题。

+0

这是什么'的findAll()'返回? Cound你展示一个示例输出? – Nurjan

+0

[关于同一行的Python打印]的可能的复制(http://stackoverflow.com/questions/5598181/python-print-on-same-line) – DeepSpace

+0

作为对重复的说,使用'打印图像的答案中的一个, '(注意',')。 – DeepSpace

回答

2

我在main_watch_images列表中获取了两个项目,只是想打印第一个项目 。

你可能会寻找break

# Extract the first image (this is always the main image) that is in the 
# "LIMITED" category 
main_watch_images = findall('<img.* src="([^"]+).*LIMITED"', dennisov_html) 
# Convert the URL to a full address 
for images in main_watch_images: 
    if images.startswith('/'): 
     images = 'https://denissov.ru' + images 
     print images 
     break 
    print images 

编辑

是啊,在这种情况下工作的,但是在这里我要打印第二个有可能是一个实例所以希望打印由它们位于其中在列表中的项目,即[0],[1]等

尝试使用list

# Extract the first image (this is always the main image) that is in the 
# "LIMITED" category 
main_watch_images = findall('<img.* src="([^"]+).*LIMITED"', dennisov_html) 
# Convert the URL to a full address 
image_list = [] 
for images in main_watch_images: 
    if images.startswith('/'): 
     images = 'https://denissov.ru' + images 
    image_list.append(images) 

number_of_images_i_want_to_show = 1 
for ctr in range(0,number_of_images_i_want_to_show): 
    print(image_list[ctr]) 
+0

是啊,在这种情况下工作,但可能有这样的情况:我想打印第二个这样希望通过打印在列表中的项目他们在哪里,即[0],[1]等 – user88720

+0

那么你应该在做任何事情之前将这些'图像'存储在一个集合中,即'list'。 'image_list.append('https://denissov.ru'+ images)'然后你可以简单地执行'print image_list [0]' – Eduard

+0

是的main_watch_images是一个列表......但是我也需要if语句域添加到缺少它的图像,然后我知道如何if语句是在for循环 – user88720