2017-05-18 1 views
0

main_watch_images 목록에 두 개의 항목이 표시되고 첫 번째 항목 만 인쇄하려고합니다. findall 줄 다음에 [0]을 추가하려고 시도했지만 모든 문자를 세로로 인쇄합니다. 나는 for 루프가 새로운 라인의 모든 것을 프린트하기 때문에 이것이라고 생각했지만, 한 라인에 모두 프린트 할 수는 없었다.Python 2.7 - 줄 바꿈없이 새 줄을 인쇄하십시오.

# 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은()'무엇을 반환하지 사용해보십시오? 샘플 출력을 보여 주시겠습니까? – Nurjan

+0

[Python print of same line]의 가능한 복제본 (http://stackoverflow.com/questions/5598181/python-print-on-same-line) – DeepSpace

+0

중복에 대한 답변 중 하나가 말하기를'print images, '(','에주의하십시오). – 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] 등의 위치에 인쇄하기를 원했습니다.

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]) 
+0

아, 그래,이 인스턴스에서는 작동하지만 두 번째 것을 인쇄하려고하는 인스턴스가있을 수 있으므로 목록의 항목을 인쇄하려고합니다. 즉 그들은 [0], [1] 등등입니다. – user88720

+0

그런 다음 다른 것들을하기 전에 컬렉션에 '이미지'를 저장해야합니다. 'image_list.append ('https://denissov.ru'+ images)'그러면 간단히 할 수 있습니다. print image_list [0]' – Eduard

+0

예 main_watch_images는 목록입니다 ...하지만 if 문도 필요합니다. 도메인을 누락 된 이미지에 추가하려면 if 문을 for 루프에 사용하는 방법을 알고있는 유일한 방법입니다. – user88720