0

I am getting two items in the main_watch_images list and just want to print the first one. I have tried to add [0] after the findall line, however it then prints every letter vertically. I figured this is because the for loop prints everything on a new line, but I can't manage to make it print all on one line.

# 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

Note this is NOT the same question as the other ones. I am an absolute beginner and cannot understand the techniques and operators etc. used in the other answers. I need a specific answer to my problem.

Eduard
  • 666
  • 1
  • 8
  • 25
user88720
  • 322
  • 6
  • 14
  • What does `findall()` return? Cound you show a sample output? – Nurjan May 18 '17 at 08:58
  • Possible duplicate of [Python print on same line](http://stackoverflow.com/questions/5598181/python-print-on-same-line) – DeepSpace May 18 '17 at 08:59
  • As one of the answers on the duplicate says, use `print images,` (note the `,`). – DeepSpace May 18 '17 at 09:00
  • It gives two websites, like such https://denissov.ru/files/collections/o_148441359756.png https://denissov.ru/files/collections/554047f513e867adb4e8a12582d6d1be3.png I would just like to print the first one by printing the first item on the list called main_watch_images. If I use [0] on this line, it prints every single letter vertically – user88720 May 18 '17 at 09:29

1 Answers1

2

I am getting two items in the main_watch_images list and just want to print the first one.

You might be looking for 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

EDIT

Ah yes that works in this instance, however there may be an instance where I want to print the second one so was hoping to print the items in the list by where they are located i.e. [0], [1] etc.

Try using a 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])
Eduard
  • 666
  • 1
  • 8
  • 25
  • Ah yes that works in this instance, however there may be an instance where I want to print the second one so was hoping to print the items in the list by where they are located i.e. [0], [1] etc. – user88720 May 18 '17 at 09:40
  • then you should probably store these 'images' in a collection i.e. `list` before doing anything else. `image_list.append('https://denissov.ru' + images)` then you can simply do `print image_list[0] ` – Eduard May 18 '17 at 09:42
  • Yes the main_watch_images is a list... But I need the if statements in there as well to add the domain to the images that are missing it, and the only way I know how to use if statements is in a for loop – user88720 May 18 '17 at 09:55
  • Thank you for that. To be honest I have been looking at this problem for too long now and have forgotten why I needed to show the links in the first place. I will have to come back to it shortly after I reassess what I need to do – user88720 May 18 '17 at 10:05
  • I would suggest that you create a new question as to not confuse others. If this answer satisfy your current problem, kindly accept it as an answer to close the question. – Eduard May 18 '17 at 10:07