상세 컨텐츠

본문 제목

파일에서 스플릿하기

python

by bumychoi 2024. 7. 5. 17:33

본문


def cnt_word1(filepath):
    with open(filepath,"r") as file:
        txt=file.read()
    txt=txt.replace(","," ")

    txt_list = txt.split(" ")
    # print(txt_list)
    return len(txt_list)

print(f'ex1 결과 : {cnt_word1("2.QnA//source//22-1.txt")}')

 

txt 파일

The adjective "deep" in deep learning refers to the use of multiple layers in the network. 
Early work showed that a linear perceptron cannot be a universal classifier,but 
that a network with a nonpolynomial activation function with one hidden layer of unbounded width can. 
Deep learning is a modern variation which is concerned with an unbounded number of layers of bounded size,which
permits practical application and optimized implementation,while retaining theoretical

정규표현식


# 방법2
import re
def cnt_word2(filepath):
    with open(filepath,"r") as file:
        txt=file.read()

    txt_list= re.split(" |,",txt)

    return len(txt_list)

print(f'결과:{cnt_word2("2.QnA//source//22-1.txt")}')

 

결과:72

 

'python' 카테고리의 다른 글

딕셔너리 생성  (0) 2024.07.05
파일 쓰기  (1) 2024.07.05
슬라이씽  (0) 2024.07.05
전역변수 & 지역변수  (0) 2024.07.05
지역변수  (0) 2024.07.05

관련글 더보기