2017-07-05 41 views
-1

使用Python 2.7我打開包含外部文件:獨特字數

Text another word1 
Lorem ipsem word1 something 
hello first word2 post 

我只希望,而不是在第三行上的字word執行獨特的計數。

所需的輸出:

$ script.py 
2x word1 
1x word2 

我得到了這麼遠,但失敗..:

import os 
import sys 
from collections import Counter 

with open('./file.txt', 'r') as file: 
     for item in file: 
      if '.sh' in item: 
        all = item.split()[2] 
        print Counter(all.split()) 
+0

你希望所有的文本以'word'和他們算什麼? –

回答

0

嘿如果您要打印所有以word開頭的單詞,這裏是代碼

import os 
import sys 

occurenceDict = {} 
with open('./file.txt', 'r') as file: 
    for line in file: # reading each line of the file 
     for word in line.split(): # splitting the line into words 
      if word.find('word') != -1: # find the occurrence of word 
       if word in occurenceDict: # check if word in dict 
        occurenceDict[word] += 1 
       else: 
        occurenceDict[word] = 1 

for word in occurenceDict: 
    print str(occurenceDict[word])+'x'+" "+word 

輸出:

2x word1 
1x word2 
+0

這一個似乎工作!謝謝。看起來相當複雜壽。 我會研究每個單詞和字符來理解代碼。 –

+0

爲了簡單起見,我會編輯答案。如果您發現它有用,請upvote並標記接受:) –

0

如果我正確理解你的問題,你要計算指定詞的occurence數量在純文本文件中。

對於這一點,我建議你打開文本文件的話

f = open("yourfile.txt","r") # Open the file 
txt = f.read()    # read it 
f.close()     # always close it 
a1 = txt.split("\n")   # split each line 
a2 = []      # create an empty array 
for i in a1:     # for each line 
    a2 += a1.split(" ")  # append every word 

然後列表簡單地使用

a2.count("yourword") 

你可以嘗試here

+0

接收:a2 + = a1.split(「」)#追加每個字 AttributeError:'list'對象沒有屬性'split' –

0

你可以試試這個:

from itertools import chain 

f = open('datafile.txt').readlines() 

f = [i.strip('\n').split() for i in f] 

f = list(chain(*f)) 

new = {i:f.count(i) for i in f if "word" in i} 

for a, b in new.items(): 
    print str(b)+"x"+" "+a