1.GPS, 2.GPS 등과 같은 .GPS 파일로 가득 찬 폴더가 있습니다. 각 파일에는 다음 다섯 줄이 있습니다.
Trace #1 at position 0.004610
$GNGSA,A,3,02,06,12,19,24,25,,,,,,,2.2,1.0,2.0*21
$GNGSA,A,3,75,86,87,,,,,,,,,,2.2,1.0,2.0*2C
$GNVTG,39.0304,T,39.0304,M,0.029,N,0.054,K,D*32
$GNGGA,233701.00,3731.1972590,S,14544.3073733,E,4,09,1.0,514.675,M,,,0.49,3023*27
... 다음 다섯 줄에 걸쳐 다른 값을 가진 동일한 데이터 구조가 이어집니다.
Trace #6 at position 0.249839
$GNGSA,A,3,02,06,12,19,24,25,,,,,,,2.2,1.0,2.0*21
$GNGSA,A,3,75,86,87,,,,,,,,,,2.2,1.0,2.0*2C
$GNVTG,247.2375,T,247.2375,M,0.081,N,0.149,K,D*3D
$GNGGA,233706.00,3731.1971997,S,14544.3075178,E,4,09,1.0,514.689,M,,,0.71,3023*2F
(위의 예에서 $ GNGSA 라인 이후의 값이 다르지 않다는 것을 알고 있습니다. 이것은 단지 나쁜 예일뿐입니다. 실제 데이터 세트에서는 다양합니다!)
"$ GNGSA"및 "$ GNVTG"로 시작하는 줄을 제거해야합니다 (즉, 각 .GPS 파일 내 5 줄의 각 그룹에서 줄 2, 3, 4를 삭제해야합니다).
이 5 줄 패턴은 각 파일 전체에서 다양한 횟수로 계속됩니다 (일부 파일의 경우 두 개의 5 줄 그룹 만있을 수 있고 다른 파일에는 수백 개의 5 줄 그룹이있을 수 있음). 따라서 행 번호를 기준으로 이러한 행을 삭제하면 작동하지 않습니다 (행 번호가 가변적이기 때문에).
내가 겪고있는 문제 (위의 예에서 볼 수 있음)는 "$ GNGSA"또는 "$ GNVTG"뒤에 오는 텍스트가 다양하다는 것입니다.
저는 현재 Python을 배우고 있습니다 (저는 v3.5를 사용하고 있습니다). 그래서 이것이 제가 몇 가지 새로운 트릭을 배우는 데 좋은 프로젝트가 될 것이라고 생각했습니다.
내가 이미 시도한 것 :
지금까지 전체 폴더를 반복하는 코드를 만들었습니다.
import os
indir = '/Users/dhunter/GRID01/' # input directory
for i in os.listdir(indir): # for each "i" (iteration) within the indir variable directory...
if i.endswith('.GPS'): # if the filename of an iteration ends with .GPS, then...
print(i + ' loaded') # print the filename to CLI, simply for debugging purposes.
with open(indir + i, 'r') as my_file: # open the iteration file
file_lines = my_file.readlines() # uses the readlines method to create a list of all lines in the file.
print(file_lines) # this prints the entire contents of each file to CLI for debugging purposes.
위의 모든 것이 완벽하게 작동합니다.
도움이 필요한 사항 :
결국 내 파일은 다음과 같아야합니다.
Trace #1 at position 0.004610
$GNGGA,233701.00,3731.1972590,S,14544.3073733,E,4,09,1.0,514.675,M,,,0.49,3023*27
Trace #6 at position 0.249839
$GNGGA,233706.00,3731.1971997,S,14544.3075178,E,4,09,1.0,514.689,M,,,0.71,3023*2F
어떤 제안이라도? 미리 감사드립니다. :)
좋습니다. Avinash Raj, tdelaney 및 Sampson Oliver의 제안에 따라 여기 Stack Overflow와 개인적으로 도움을 준 다른 친구는 현재 작동중인 솔루션입니다.
import os
indir = '/Users/dhunter/GRID01/' # input directory
for i in os.listdir(indir): # for each "i" (iteration) within the indir variable directory...
if i.lower().endswith('.gps'): # if the filename of an iteration ends with .GPS, then...
if not i.lower().endswith('.gpsnew.gps'): # if the filename does not end with .gpsnew.gps, then...
print(i + ' loaded') # print the filename to CLI.
with open (indir + i, 'r') as my_file:
for line in my_file:
if not line.startswith('$GNGSA'):
if not line.startswith('$GNVTG'):
with open(indir + i + 'new.gps', 'a') as outputfile:
outputfile.write(line)
outputfile.write('\r\n')
( "if not i.lower (). endswith ( '. gpsnew.gps') :"스크립트의 이전 사용에서 출력 파일을 사용하지 못하도록 if 문의 다른 레이어를 추가해야했습니다. 그러나이 줄은 나중에이 지침을 사용하는 사람이라면 누구나 쉽게 삭제할 수 있습니다.)
마지막 세 번째 줄의 열기 모드를 추가를 위해 "a"로 전환하여 매번 덮어 쓰지 않고 파일에 올바른 줄을 모두 저장합니다.
또한 마지막 줄에 추가하여 각 줄 끝에 줄 바꿈을 추가했습니다.
도움, 설명 및 제안에 감사드립니다. 이 솔루션이 미래에 누군가에게 유용하기를 바랍니다. :)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다