2014-11-04 6 views
0

문자열 목록을 선택,이 방법으로 테이블을 채우는해야합니다평 : 나는 루프 내부의 선택 문을 사용하려고 해요

<tr py:for="i in range(0,25)"> 
    <py:choose my_list[i]='0'> 
     <py:when my_list[i]='0'><td>NOT OK</td></py:when> 
     <py:otherwise><td>OK</td></py:otherwise> 
    </py:choose> 
... 
... 
</tr> 

I 라인 <py:choose...>에 오류가 있습니다

TemplateSyntaxError: not well-formed (invalid token): line... 

하지만 choose 문을 사용하는 방법을 잘 이해할 수 없습니다!

<tr py:for="i in range(0,25)"> 
    <py:choose my_list[i]> 
     <py:when my_list[i]='0'><td>NOT OK</td></py:when> 
     <py:otherwise><td>OK</td></py:otherwise> 
    </py:choose> 
... 
... 
</tr> 

당신이 날 도와 드릴까요 : 나는 C-등 생각한다면 난 단지 쓸 필요 (그리고 더 논리적 인 나에게 것)? 아, my_list은 문자열 목록입니다. 그런 다음 문자열이 0 인 경우 나에게 적합하지 않다면 나머지는 모두 정상입니다.

답변

0

py:choose 내에서 Ith my_list 항목에 액세스 할 수 없습니다. 대신 i은 범위에서 int과 같게 설정됩니다. 이것은 고의적 인 예제이며 Ith 값인 my_list에 액세스하려고하고 있다고 가정합니다. 이 경우 range 대신에 my_list을 반복해야합니다.

다음은 현재 methodolgies를 사용하는 예입니다. 이 오류는 py:choose 자체 내에 :

from genshi.template import MarkupTemplate 
template_text = """ 
<html xmlns:py="http://genshi.edgewall.org/" > 
    <tr py:for="index in range(0, 25)"> 
     <py:choose test="index"> 
      <py:when index="0"><td>${index} is NOT OK</td></py:when> 
      <py:otherwise><td>${index} is OK</td></py:otherwise> 
     </py:choose> 
    </tr> 
</html> 
""" 
tmpl = MarkupTemplate(template_text) 
stream = tmpl.generate() 
print(stream.render('xhtml')) 

그러나, 당신은 아마 my_listlist_of_ints을 변경해야하고, 직접 반복. 당신이 my_list 내에서 각 항목의 인덱스를 알고 있어야하는 경우, enumerate를 사용심지어 더 나은 :

물론
from genshi.template import MarkupTemplate 
template_text = """ 
<html xmlns:py="http://genshi.edgewall.org/" > 
    <tr py:for="(index, item) in enumerate(list_of_ints)"> 
     <py:choose test="index"> 
      <py:when index="0"><td>index=${index}, item=${item} is NOT OK</td></py:when> 
      <py:otherwise><td>${index}, item=${item} is OK</td></py:otherwise> 
     </py:choose> 
    </tr> 
</html> 
""" 
tmpl = MarkupTemplate(template_text) 
stream = tmpl.generate(list_of_ints=range(0, 25)) 
print(stream.render('xhtml')) 

, 이러한 예는 파이썬 인터프리터에서 실행되도록 만들어졌다. 이 설정을 수정하여 설정 작업을 쉽게 할 수 있습니다.

HTH

+0

고마워요! 우리는 해결책에 가깝습니다 ... 나는 '0'이라고 쓰여졌을 때, 필자는 필자가 알아야 할 점이 있습니다 : my_list [i] 값이 아니라 인덱스에있을 때. 이제 나는 같은 오류가 있지만 py : 행에, 왜냐하면 내가 쓰고 있기 때문에 :''py : choose test = "my_list [i]"> user2174050

+0

으로 트리밍 한 경우라도 열거 형 또는 루프 용을 이해하지 못했을 수 있습니다. 두 번째 예제를 원하는대로 변경하려면'list_of_ints'를'my_list'로 변경하고''py : choose test = "index">'를''로 대체하십시오. – VooDooNOFX