2014-01-14 5 views
15

ich mit dem folgenden Codewort einer txt-Datei verlasse mich:Wort aus einer txt-Datei Programm zählen

#!/usr/bin/python 
file=open("D:\\zzzz\\names2.txt","r+") 
wordcount={} 
for word in file.read().split(): 
    if word not in wordcount: 
     wordcount[word] = 1 
    else: 
     wordcount[word] += 1 
print (word,wordcount) 
file.close(); 

das mir die Ausgabe wie folgt schenkt:

>>> 
goat {'goat': 2, 'cow': 1, 'Dog': 1, 'lion': 1, 'snake': 1, 'horse': 1, '': 1, 'tiger': 1, 'cat': 2, 'dog': 1} 

aber ich möchte, dass die Ausgang auf die folgende Weise:

word wordcount 
goat 2 
cow  1 
dog  1..... 

auch ein zusätzliches Symbol in der Ausgabe () ich erhalte. Wie kann ich das entfernen?

+0

einen Blick auf [string Formatierung] (http://docs.python.org/2/library/string.html#formatspec). –

Antwort

34

Die lustigen Symbole, denen Sie begegnen, sind ein UTF-8 BOM (Byte Order Mark). Um loszuwerden von ihnen, öffnen Sie die Datei die richtige Codierung verwenden (Ich gehe davon aus, du bist auf Python 3):

file = open(r"D:\zzzz\names2.txt", "r", encoding="utf-8-sig") 

Des Weiteren zum Zählen, können Sie collections.Counter:

from collections import Counter 
wordcount = Counter(file.read().split()) 

Es werden ihnen ist auch einfach:

>>> for item in wordcount.items(): print("{}\t{}".format(*item)) 
... 
snake 1 
lion 2 
goat 2 
horse 3 
1
import sys 
file=open(sys.argv[1],"r+") 
wordcount={} 
for word in file.read().split(): 
    if word not in wordcount: 
     wordcount[word] = 1 
    else: 
     wordcount[word] += 1 
for key in wordcount.keys(): 
    print ("%s %s " %(key , wordcount[key])) 
file.close(); 
+0

Was ist Ihre Python-Version? – duck

+0

Ich habe ein vollständiges Beispiel gegeben, ob das funktioniert – duck

+0

Sie können die Beispielgültigkeit unter http://www.compileonline.com/execute_python_online.php überprüfen – duck

29
#!/usr/bin/python 
file=open("D:\\zzzz\\names2.txt","r+") 
wordcount={} 
for word in file.read().split(): 
    if word not in wordcount: 
     wordcount[word] = 1 
    else: 
     wordcount[word] += 1 
for k,v in wordcount.items(): 
    print k, v 
2

Wenn Sie graphLab verwenden, können Sie diese Funktion verwenden. Es ist wirklich leistungsfähige

products['word_count'] = graphlab.text_analytics.count_words(your_text) 
0
FILE_NAME = 'file.txt' 

wordCounter = {} 

with open(FILE_NAME,'r') as fh: 
    for line in fh: 
    # Replacing punctuation characters. Making the string to lower. 
    # The split will spit the line into a list. 
    word_list = line.replace(',','').replace('\'','').replace('.','').lower().split() 
    for word in word_list: 
     # Adding the word into the wordCounter dictionary. 
     if word not in wordCounter: 
     wordCounter[word] = 1 
     else: 
     # if the word is already in the dictionary update its count. 
     wordCounter[word] = wordCounter[word] + 1 

print('{:15}{:3}'.format('Word','Count')) 
print('-' * 18) 

# printing the words and its occurrence. 
for (word,occurance) in wordCounter.items(): 
    print('{:15}{:3}'.format(word,occurance)) 
#
Word   Count 
    ------------------ 
    of    6 
    examples   2 
    used    2 
    development  2 
    modified   2 
    open-source  2 
0
#!/usr/bin/python 
file=open("D:\\zzzz\\names2.txt","r+") 
wordcount={} 
for word in file.read().split(): 
    if word not in wordcount: 
     wordcount[word] = 1 
    else: 
     wordcount[word] += 1 

for k,v in wordcount.items(): 
    print k,v 
file.close(); 
+2

Bitte fügen Sie eine Erklärung hinzu. –

Verwandte Themen