728x90
반응형
1번 문제 |
t=0 filename=input("파일 이름을 입력하세요") infile=open(filename,"r") for line in infile: line=line.strip() c=len(line) t=t+c print("파일안에는 총",t,"개의 글자가 있습니다") |
2번 문제 |
infilename = input("파일 이름을 입력하세요: ").strip() infile = open(infilename, "r") file_s = infile.read() remove = input("삭제할 문자열을 입력하세요: ").strip() modified_s = file_s.replace(remove, "")# 삭제할 문자열을 ""로 대체 infile.close() outfile = open(infilename, "w")# 쓰기 형식으로 파일열기 print(modified_s, file = outfile, end = "") print("변경된 파일이 저장되었습니다.") outfile.close() |
3번 문제 |
filename = input("입력 파일 이름: ").strip() infile = open(filename, "r") def func(line, counter): for c in line: if c.isalpha():#영문자인지를 판단 if c in counter: counter[c] = counter[c] + 1# 같은 문자 수를 카운트 else: counter[c] = 1 dic = { } for line in infile: func(line, dic) print(dic) infile.close() |
4번 문제 |
import pickle #pickle모듈을 사용해 저장 outfile = open("test.dat", "wb")#저장장소 설정 pickle.dump(12, outfile) pickle.dump(3.14, outfile) pickle.dump([1, 2, 3, 4, 5], outfile) outfile.close() #pickle모듈을 사용해 읽기 infile = open("test.dat", "rb")#읽을파일 지정 print(pickle.load(infile)) print(pickle.load(infile)) print(pickle.load(infile)) infile.close() |
5번 문제 |
a = input("입력 파일 이름: ") b = input("출력 파일 이름: ") infile = open(a, "r") outfile = open(b, "w") t = 0.0 c = 0 line = infile.readline() while line != "" : k = float(line) t = t + k c = c + 1 line = infile.readline() outfile.write("합계="+ str(t)+"\n") avg = t / c outfile.write("평균="+ str(avg)+"\n") infile.close() outfile.close() |
6번 문제 |
import pickle from tkinter import * phonebook = { } current = 0 name = "" phone = "" window = Tk() frame1 = Frame(window) frame1.pack() Label(frame1, text = "이름 ").grid(row = 1, column = 1, sticky = W) nameEntry = Entry(frame1, textvariable = name, width = 30) nameEntry.grid(row = 1, column = 2) frame2 = Frame(window) frame2.pack() Label(frame2, text = "전화번호").grid(row = 1, column = 1, sticky = W) phoneEntry = Entry(frame2, textvariable = phone, width = 30) phoneEntry.grid(row = 1, column = 2) frame3 = Frame(window) frame3.pack() def save(): outfile = open("phonebook.dat", "wb") pickle.dump(phonebook, outfile) print("주소들이 파일에 저장되었습니다") outfile.close() def load(): infile = open("phonebook.dat", "rb") phonebook = pickle.load(infile) infile.close() print("파일에서 주소를 읽었습니다.") go_first() def add(): phonebook[nameEntry.get()] = phoneEntry.get() print(phonebook) save() def go_first(): global current current = 0 ks = list(phonebook) print(phonebook) nameEntry.delete(0, END) nameEntry.insert(0, ks[current]) phoneEntry.delete(0, END) phoneEntry.insert(0, phonebook[ks[current]]) def go_next(): global current current += 1 ks = list(phonebook) nameEntry.delete(0, END) nameEntry.insert(0, ks[current]) phoneEntry.delete(0, END) phoneEntry.insert(0, phonebook[ks[current]]) def go_previous(): global current current -= 1 ks = list(phonebook) nameEntry.delete(0, END) nameEntry.insert(0, ks[current]) phoneEntry.delete(0, END) phoneEntry.insert(0, phonebook[ks[current]]) def go_last(): global current current = 5 ks = list(phonebook) print(phonebook) nameEntry.delete(0, END) nameEntry.insert(0, ks[current]) phoneEntry.delete(0, END) phoneEntry.insert(0, phonebook[ks[current]]) b1 = Button(frame3, text = "추가", command = add).grid(row = 1, column = 1) b2 = Button(frame3, text = "처음", command = go_first).grid(row = 1, column = 2) b3 = Button(frame3, text = "다음", command = go_next).grid(row = 1, column = 3) b4 = Button(frame3, text = "이전", command =go_previous).grid(row = 1, column = 4) b5 = Button(frame3, text = "마지막", command = go_last).grid(row = 1, column = 5) b6 = Button(frame3, text = "파일 읽기", command = load).grid(row = 1, column = 6) window.mainloop() |
728x90
반응형
'BIG DATA STUDY > 두근두근파이썬 문제풀이' 카테고리의 다른 글
두근두근 파이썬 연습문제 10장 (0) | 2022.04.08 |
---|---|
두근두근 파이썬 연습문제 9장 (0) | 2022.01.16 |
두근두근 파이썬 연습문제 7장 (1) | 2022.01.16 |
두근두근 파이썬 연습문제 6장 (1) | 2022.01.15 |
두근두근 파이썬 연습문제 5장 (0) | 2022.01.15 |
댓글