2014-09-20 2 views
0

저는 python을 처음 사용합니다. 이 코드 스 니펫은 길이가 25 (dev_info_lines) 인 목록을 가져 와서 문자열의 일부를 찾아서 dev 객체의 속성에 할당합니다. 그것은 매우 직설적 인 것처럼 보입니다. 그러나 for 루프는 목록에 25 줄이 있더라도 한 번만 반복됩니다. 왜?for 루프 문자열 비교기는 한 번만 반복됩니다

나는 확실한 무엇인가 놓치고 있다고 확신한다. 나는 어제 파이썬으로 코딩하기 시작했다.

편집 : 내 코드가 커뮤니티에서 발견 한 오류로 수정되었습니다.이 작동 기능이 다른 사람에게 유용 할 수도 있습니다. (당신이 궁금해하는 경우에, 내 반환 dev에 줄은 안으로 들어갔다 for 루프, 끝내고 기능을 조기에. 신인 한 실수!) 모두에게 감사드립니다!

EDIT2 : 전체 프로젝트에 대한 링크에 관심이있는 사람은 누구나 here을 찾을 수 있습니다. FreeNAS의 드라이브 온도 및 SMART 테스트 결과에 대한 전자 메일 보고서를 보냅니다.

def SortInfo(device_id): 
    dev_info_lines = (a command that returns a list of 25 lines about hard drive SMART info) 
    bEnteredInfoSection = False 
    i=0 
    for line in dev_info_lines: 
     i=i+1 
     if (not bEnteredInfoSection): 
      TheFirstField = string.split(line," ",2) 
      if ("information section" in line.lower()): 
       bEnteredInfoSection = True 
     else: 
      field = string.split(line,":",1) 
      if (field[0].lower() == "model family"): 
       dev.family = field[1].strip() 
      elif (field[0].lower() == "device model"): 
       dev.model = field[1].strip() 
      elif (field[0].lower() == "serial number"): 
       dev.serial = field[1].strip() 
      elif (field[0].lower() == "firmware version"): 
       dev.firmware_version = field[1].strip() 
      elif (field[0].lower() == "user capacity"): 
       dev.capacity = field[1].strip() 
      elif (field[0].lower() == "sector sizes"): 
       dev.sector_sizes = field[1].strip() 
      elif (field[0].lower() == "rotation rate"): 
       dev.rotation_rate = field[1].strip() 
      elif (field[0].lower() == "device is"): 
       dev.device_is = field[1].strip() 
      elif (field[0].lower() == "ata version is"): 
       dev.ata_version = field[1].strip() 
      elif (field[0].lower() == "sata version is"): 
       dev.sata_version = field[1].strip() 
      elif (field[0].lower() == "smart support is"): 
       temp = string.split(field[1].strip()," ",1) 
       strTemp = temp[0].strip().lower() 
       if (strTemp == "available"): 
        dev.smart_support_available = True 
       elif (strTemp == "unavailable"): 
        dev.smart_support_available = False 
       elif (strTemp == "enabled"): 
        dev.smart_support_enabled = True 
       elif (strTemp == "disabled"): 
        dev.smart_support_enabled = False 
    return dev 
+2

'for for dev_info_lines.split ('\ n') ' – jordanm

+0

'dev_info_lines'는 전체 25 줄로 구성된 하나의 긴 문자열 일 수 있습니다. 적절하게 분할해야 할 수도 있습니다. – Abhi

+1

또한'.splitlines'입니다. – Veedrac

답변

1

Python은 특히 텍스트 처리를 위해 print 문을 사용하여 디버깅하는 것이 매우 편리합니다.

당신이 필요합니다 :

  • 인쇄 단지 루프 전에
  • 인쇄 렌 (dev_info_lines를) dev_info_lines의 시작 부분에
  • 인쇄 라인 (당신이 생각하는 것입니다 있는지 확인하기 위해) 반복

그것을 반복하는 즉시 명백 할 것이다 아니에요, 당신은 우리를 위해 다음 구체적인 질문이 이유의 대답 :

,
+0

. 그러나 나는 이미 이것을했다. dev_info_line은 25 개의 문자열 목록이지만 for 루프는 한 번 이상 반복하지 않으며 한 시간 더 실험하면 왜 그 이유가 명확하지 않습니다. for 루프가 일찍 종료 될 수있는 원인은 무엇입니까? –

+1

첫 번째 반복의 시작 부분에 "줄"이란 무엇입니까? – GreenAsJade

+1

붙여 넣은 코드가 실행중인 코드와 분명히 다릅니다. dev_info_lines가 생성되는 방법 (주석만큼)을 주석 처리했을뿐만 아니라 SortInfo 선언이 작동하지 않을 수도 있습니다. 이렇게하면 디버그하는 데 어려움이 있습니다. 우리가 알고있는 모든 것에 대해 들여 쓰기가 완전히 잘못되었을 수 있습니다. 파이썬 파일이 다르게 들여 쓰여진 경우에는 루프가 전혀 없을 수도 있습니다. 여기에서 좋은 도움을 얻으려면 실행중인 파일을 _exactly_ 게시해야합니다. _and_를 직접 붙여 넣을 수있을 때까지 실행중인 작업을 제거해야합니다. – GreenAsJade

관련 문제