binary 와 text

pickle 사용

import pickle

bfilename = './test.bin'
tfilename = './test.txt'

data1 = 77
data2 = "hello world"
data3 =['car','apple','house']

binary와 텍스트 쓰기

with open(bfilename, 'wb') as f:
    pickle.dump(data1,f) #dumps (문자열 직렬화)
    pickle.dump(data2,f)
    pickle.dump(data3,f)

with open(tfilename,'wt') as f:
    f.write(str(data1))
    f.write('\n')
    f.write(data2)
    f.write('\n')
    f.writelines('\n'.join(data3)) #리스트는 write로 안써짐

binary 읽기

with open(bfilename,'rb') as f:
    b = pickle.load(f) #loads (문자열 역직렬화)
    print(type(b), b)
    b= pickle.load(f)
    print(type(b), b)
    b= pickle.load(f)
    print(type(b), b)
  <class 'int'> 77
<class 'str'> hello world
<class 'list'> ['car', 'apple', 'house']

텍스트 읽기

with open(tfilename, 'rt') as f:
    for i, line in enumerate(f,1):
        print(type(line), 'text' +str(i) +'|',line,end='')
  <class 'str'> text1| 77
<class 'str'> text2| hello world
<class 'str'> text3| car
<class 'str'> text4| apple
<class 'str'> text5| house
  • binary는 자료형도 그대로 유지함
  • 텍스트는 읽어오면 그대로 텍스트임

Tags:

Updated:

Leave a Comment