1. 程式人生 > >Python 獲取檔案中單詞數,字元,字元數

Python 獲取檔案中單詞數,字元,字元數

# Get all words list
f = open('nihao')
wordList = [word for line in f for word in line.split()]

# Get number of words
wordListLen = len(wordList)
f.close()

# Get number of chars
f = open('nihao')
charLen = sum([len(word) for line in f for word in line.split()])
f.close()