2012-02-09 2 views
0

텍스트 문서에서 POLYLINE 단어를 검색하는 방법을 찾은 다음 텍스트 파일에서 X 축 및 Y 좌표와 같은 POLYLINE 특성을 계속 검색하는 방법을 찾은 후 다음 POLYLINE을 찾는 방법에 대해 궁금합니다. 다시해라.라인을 일치시킨 후, 어떻게 더 많은 라인을 읽고 값을 기록한 다음 다시 시작합니까? Python

  1. 폴리 라인
  2. X의 COORD
  3. 오십
  4. Y의 COORD
  5. 예순셋
  6. 을 :

    나는 다음과 같습니다 텍스트 파일이

  7. blah
  8. X COORD
  9. 아흔
  10. Y의 COORD
  11. 여섯
  12. 폴리 라인
  13. 등등 ...

내 모든 코드는 지금까지 단어 폴리 라인을 찾을 것입니다 않습니다, I 폴리 라인 (POLYLINE)의 속성을 수집하려고 시도했습니다. 여기 내 코드는 지금까지 있습니다 :

import re 

fileName = open("textdoc.txt, "r") 



for line in fileName: 
    if re.match("POLYLINE", line): 
     print line 



fileName.close() 

어떻게이 문제를 해결할 수 있습니까?

답변

0
for line in fileName: 
    if re.match("POLYLINE", line): 
     for line in filename: 
      if re.match(xcoord,line): 
       dostuff() 
      if re.match(ycoord,line): 
       dostuff() 

좌표를 실제로 어떻게 찾을 수 있었는지에 관해서는 우리가 제공 한 것과 함께 무엇이든하는 것은 어렵습니다. 좌표가 나타날 라인에 대한 패턴이 없거나 좌표에없는 다른 숫자가 있고 그 숫자에 일종의 식별이없는 경우에는 할 수있는 일이 많지 않습니다. 기본적으로, 좌표를 다른 것과 구별 할 수있는 것이 무엇이든 찾아내는 것입니다.

0

구조가 일관성이 있다고 가정하면 아래와 같은 속성을 수집 할 수 있습니다.

#store polylines in a list for future use 
polylines=[] 

dataFile = open('textdoc.txt') 

#collect the attributes in dictionaries 
attrs={} 

#because it appears you need to look one line ahead to get the coordinates 
# it would be easiest to read all lines into a list 

datalines = dataFile.readlines() 
for idx, line in enumerate(datalines): 
    #handle polyline flags by storing the previous attributes 
    if 'POLYLINE' in line: 
     #attrs will evaluate to True if its not empty 
     if attrs: 
      #append the old polyline attributes and start a new one 
      polylines.append(attrs) 
      attrs = {} 

     continue 

    #collect the attributes from the line following the coord flag 
    # of course this breaks real fast if the file structure changes 
    if 'xcoord' in line: 
     #grab the coordinate from the following line 
     attrs['xcoord'] = datalines[idx + 1].strip() 
     continue 

    if 'ycoord' in line: 
     attrs['ycoord'] = datalines[idx + 1].strip() 
     continue 
관련 문제