Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 파이썬크롤링
- 파이썬차트
- sql연습
- SQLSCOTT
- 주피터노트북그래프
- 판다스데이터분석
- sql연습하기
- 주피터노트북데이터분석
- 파이썬알고리즘
- 데이터분석시각화
- python알고리즘
- 파이썬시각화
- sql따라하기
- 수업기록
- 주피터노트북맷플롯립
- 맷플롯립
- Python
- python데이터분석
- 파이썬수업
- 팀플기록
- 파이썬데이터분석주피터노트북
- matplotlib
- python수업
- 판다스그래프
- SQL
- SQL수업
- 주피터노트북
- 파이썬
- 파이썬데이터분석
- 주피터노트북판다스
Archives
- Today
- Total
IT_developers
Python 개념 및 실습 - 파일 읽고 쓰기(2) 본문
data 폴더 안에 넣기
review.txt
0.00MB
sample.xlsx
0.01MB
sample1.csv
0.00MB
sample2.csv
0.00MB
sample3.csv
0.00MB
score.txt
0.00MB
users.json
0.01MB
word.txt
0.08MB
파일 읽기
with open("data/review.txt", "r", encoding="utf-8") as f:
print(f.read())
readline() : 줄 단위로 읽어오기
with open("data/review.txt", "r", encoding="utf-8") as f:
print(f.readline())
with open("data/review.txt", "r", encoding="utf-8") as f:
line = f.readline()
while line:
print(line, end=" ")
line = f.readline()
readlines : 파일의 내용을 리스트로 가져오기
with open("data/review.txt", "r", encoding="utf-8") as f:
print(f.readlines())
score.txt 읽어서 평균 출력하기
score = []
with open("data/score.txt", "r", encoding="utf-8") as f:
for num in f:
score.append(int(num))
print(score)
print("평균 : %.2f" % (sum(score) / len(score)))
총합과 평균을 result.txt 파일로 작성(ex 총합 : 520,평균 : 86.67)
with open("data/result.txt", "w", encoding="utf-8") as f:
f.write("총합 : %.d\n" % sum(score))
f.write("평균 : %.2f" % (sum(score) / len(score)))
info.txt 읽은 후 BMI 지수를 계산한 후 화면 출력
# info.txt는 엔터를 기준으로 입력이 되어 있음
# 나바, 96,140
# 이름 : 나바
# 몸무게 : 96
# 키 : 140
# BMI : 계산값
# 결과 : 저체중
# BMI = weight / (height/100) ** 2
# 결과 bmi 지수가 25이상이면 과체중, 18.5 이상이면 정상체중, 나머지 저체중
with open("data/info.txt", "r", encoding="utf-8") as f:
for info in f:
name, weight, height = info.strip().split(",") # strip() : 공백 제거, split(","): , 기준으로 잘라내기
print(name, weight, height) # , 없이 이름 몸무게 키 출력. 자료 1000개가 다 출력됨



with open("data/info.txt", "r", encoding="utf-8") as f:
for info in f:
name, weight, height = info.strip().split(",")
print(name, weight, height)
bmi = int(weight) / (int(height) / 100) ** 2
result = ""
if 25 <= bmi:
result = "과체중"
elif 18.5 <= bmi:
result = "정상체중"
else:
result = "저체중"
print(
"\n".join(["이름 : {}", "몸무게 : {} ", "키 : {}", "BMI : {}", "결과 : {}"]).format(
name, weight, height, bmi, result
)
)
print()



원본 파일을 읽은 후 암호화, 암호화된 파일을 읽은 후 복호화 프로그램 작성
data 폴더에 origin.txt 저장
content = ""
while True:
no = int(input("1.암호화 | 2.복호화 | 3.종료 중 메뉴 입력 : "))
print()
if no == 1:
# origin 읽은 후 content 라는 변수에 읽어온 내용 담기
with open("data/origin.txt", "r", encoding="utf-8") as f:
content = f.read()
# encryption.txt 작성
# 읽어온 내용 암호화 ord("c") : 99, (코드값 + 100) => 문자로 변경 chr(99) : c
# chr(ord(안) +100)
with open("data/encryption.txt", "w", encoding="utf-8") as f:
for c in content:
f.write(chr(ord(c) + 100))
elif no == 2:
# encryption.txt 읽은 후 원래 있던 내용으로 출력
with open("data/encryption.txt", "r", encoding="utf-8") as f:
content = f.read()
# 하나씩 읽어오기
for i in range(0, len(content)):
print(chr(ord(content[i]) - 100), end="")
else:
print("프로그램 종료")
break
r,w, 이진 파일(동영상, 이미지) - rb,wb
# c-windows-notepad.exe 복사하기
data = None
with open("c:\\windows\\notepad.exe", "rb") as f:
data = f.read()
with open("c:\\temp\\notepad.exe", "wb") as f:
f.write(data)
'Python' 카테고리의 다른 글
Python 개념 및 실습 - 파일 읽고 쓰기(4) (0) | 2022.09.10 |
---|---|
Python 개념 및 실습 - 파일 읽고 쓰기(3) (0) | 2022.09.10 |
Python 개념 및 실습 - 파일 읽고 쓰기(1) (0) | 2022.09.09 |
Python 개념 및 실습 - 함수(2) (0) | 2022.09.08 |
Python 개념 및 실습 - 함수(1) (0) | 2022.09.08 |
Comments