python

파일에서 스플릿하기

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