PYTHON
텍스트 파일 읽어오기 및 슬라이싱
2016. 4. 10. 18:41
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 29 30 31 32 | # 파일을 읽어와서 4가지 자료형으로 저장하기. # 1. 읽어오기 file = open('testtest','r',encoding='utf-8') str = file.read() listkey = str.split('\n') setkey = set(str.split('\n')) tuplekey = tuple(str.split('\n')) print(listkey) listkeyTest = [] for i in listkey: listkeyTest.append(i.split(':')) print(listkeyTest) print() print(listkeyTest) dictkey = dict(listkeyTest) print(dictkey) dic = {} for i in listkey: print(i) print(type(i)) a = i[:i.find(':')] print('i.find = ',i.find(':')) b = i[i.find(':'):] dic[a] = b print(dic) | cs |
find함수를 써서 지정한 다음에 string의 : 를 이용해서 전, 후를 잘라서 읽어오는 것.
웹 크롤링에 적용 하면 좋다!