lines = open('filename').read().split('\n')
lines = open('filename').readlines()
.split()은 무언가를 나누는 건데... 띄어쓰기할때마다 나눈다는 의미인듯 싶다..#3 :for문과 if 문 쓰임
3. for문의 응용
"총 5명의 학생이 시험을 보았는데 시험 점수가 60점이 넘으면 합격이고 그렇지 않으면 불합격이다. 합격인지 불합격인지 결과를 보여주시오."
marks = [90, 25, 67, 45, 80]
# marks1.py
marks = [90, 25, 67, 45, 80]
number = 0
for mark in marks:
number = number +1
if mark >= 60:
print("%d번 학생은 합격입니다." % number)
else:
print("%d번 학생은 불합격입니다." % number)
C:\doit>python marks1.py
1번 학생은 합격입니다.
2번 학생은 불합격입니다.
3번 학생은 합격입니다.
4번 학생은 불합격입니다.
5번 학생은 합격입니다
=================================================================
import matplotlib.pyplot as plt #자료를 차트나 플롯으로 시각화하는 패키지import matplotlib.animation as animation from matplotlib import style style.use('fivethirtyeight') #figure와 subplots에 관한 내용 : 바탕을 의미하는듯fig = plt.figure() ax1 = fig.add_subplot(1,1,1) #1 def animate(i) : graph_data = open('example.txt','r').read() lines = graph_data.split('\n') #아마 띄어쓰기마다.. xs = [] ys = [] for line in lines: if len(line) > 1: x, y = line.split(',') xs.append(float(x)) ys.append(float(y)) ax1.clear() # 내용들이 추가 되니까...이미 나온 내용들을 제거한다.. ax1.plot(xs, ys)
ani = animation.FuncAnimation(fig, animate, interval=1000) #2
plt.show()