1
입력 파일에서 "ND"로 시작하는 라인을 찾고 있습니다. 은 다음과 같습니다이 :입력 파일에서 정확한 문자열 찾기
ND 195 4.53434033e+006 5.62453069e+006 2.56369141e+002
ND 196 4.53436645e+006 5.62443565e+006 2.56452118e+002
NS 129 113 97 82 58 59 37 22 17 18
NS 5 6 12 26 42 64 62 85 102 117
는이 같은 코드 작성 :
from __future__ import print_function
found_ND = False
text = "ND"
with open('input.txt', 'r') as f, open('output.dat', 'w') as outfile:
for line in f:
if text in line:
found_ND = True
if found_ND:
#do whatever you want
try:
line = line.strip()
columns = line.split()
i = float(columns[1])
x = float(columns[2])
y = float(columns[3])
z = float(columns[4])
print(z)
print("{:.2f}".format(z), file=outfile)
except ValueError:
pass
을하지만 결과에 나는 또한 "NS"로 시작하는 문자열의 네 번째 열을 얻는다. 결과는 다음과 같습니다
256.37
256.45
82.00
26.00
어떻게 "NS"
로 시작하는 라인을 피하기 위해 코드를 작성 할 수 있습니까?
만약 당신이'line.startswith ('ND') try ....'를 의미합니까? –