본문 바로가기

Python

[Python] pickle 파일 작성 및 읽기

Pickle 파일 작성

import pickle
data = {
	"계획1" : "논문 스터디",
    	"계획2" : "중국어 공부"
}
file = open("data.pickle", "wb")  # wb: wrtie binary -> binary 형태로, 컴퓨터가 바로 읽을 수 있음
pickle.dump(data, file)  # data를 file에 담음
file.close()

 

Pickle 파일 읽기

import pickle
file = open("data.pickle", "rb")
data = pickle.load(file)
print(data)
file.close()

 

With 구문

# with 구문 X
file = open("data.txt", "r", encoding = "utf8")
data = file.read()
file.close()

# with 구문 O
with open("file.txt", "r", encoding = "utf8") as file:
	data = file.read()

 

'Python' 카테고리의 다른 글

[Python] 동시성(Concurrency), 병렬성(Parallelism)  (0) 2024.01.29
[Python] csv 파일 쓰기, 읽기  (0) 2024.01.29