计算Python文件中的字数
我正试图计算一个文件中出现几个单词的实例数 这是我的密码:计算Python文件中的字数,python,python-3.x,Python,Python 3.x,我正试图计算一个文件中出现几个单词的实例数 这是我的密码: #!/usr/bin/env python file = open('my_output', 'r') word1 = 'wordA' print('wordA', file.read().split().count(word1)) word2 = 'wordB' print('wordB', file.read().split().count(word2)) word3 = 'wordC' print('wordC', file.
#!/usr/bin/env python
file = open('my_output', 'r')
word1 = 'wordA'
print('wordA', file.read().split().count(word1))
word2 = 'wordB'
print('wordB', file.read().split().count(word2))
word3 = 'wordC'
print('wordC', file.read().split().count(word3))
代码中的问题是它只统计word1的实例数。如何修复此代码以计算word2和word3
谢谢 主要问题是
file.read()
使用文件。因此,第二次搜索时,最终搜索的是一个空文件。最简单的解决方案是读取一次文件(如果文件不太大),然后只搜索以前读取的文本:
#!/usr/bin/env python
with open('my_output', 'r') as file:
text = file.read()
word1 = 'wordA'
print('wordA', text.split().count(word1))
word2 = 'wordB'
print('wordB', text.split().count(word2))
word3 = 'wordC'
print('wordC', text.split().count(word3))
为了提高性能,也可以只拆分一次:
#!/usr/bin/env python
with open('my_output', 'r') as file:
split_text = file.read().split()
word1 = 'wordA'
print('wordA', split_text.count(word1))
word2 = 'wordB'
print('wordB', split_text.count(word2))
word3 = 'wordC'
print('wordC', split_text.count(word3))
将
与
一起使用还可以确保文件在读取后正确关闭。主要问题是file.read()
会占用文件。因此,第二次搜索时,最终搜索的是一个空文件。最简单的解决方案是读取一次文件(如果文件不太大),然后只搜索以前读取的文本:
#!/usr/bin/env python
with open('my_output', 'r') as file:
text = file.read()
word1 = 'wordA'
print('wordA', text.split().count(word1))
word2 = 'wordB'
print('wordB', text.split().count(word2))
word3 = 'wordC'
print('wordC', text.split().count(word3))
为了提高性能,也可以只拆分一次:
#!/usr/bin/env python
with open('my_output', 'r') as file:
split_text = file.read().split()
word1 = 'wordA'
print('wordA', split_text.count(word1))
word2 = 'wordB'
print('wordB', split_text.count(word2))
word3 = 'wordC'
print('wordC', split_text.count(word3))
将
与
一起使用也将确保文件在读取后正确关闭。在代码中,文件在第一行被消耗(耗尽),因此下一行将不会返回任何要计算的内容:第一行file.read()
读取文件的全部内容并将其作为字符串返回。第二个文件.read()
没有什么可读的,只返回一个空字符串'
——第三个文件.read()
这是一个可以满足您需要的版本:
from collections import Counter
counter = Counter()
with open('my_output', 'r') as file:
for line in file:
counter.update(line.split())
print(counter)
您可能需要进行一些预处理(以去除特殊字符和、
和
以及其他字符)
在python标准库中,对于这类事情非常有用
请注意,这样您只需在文件上迭代一次,而不必在任何时候将整个文件存储在内存中
如果您只想跟踪某些单词,您可以只选择它们,而不是将整行内容传递给计数器:
from collections import Counter
import string
counter = Counter()
words = ('wordA', 'wordB', 'wordC')
chars_to_remove = str.maketrans('', '', string.punctuation)
with open('my_output', 'r') as file:
for line in file:
line = line.translate(chars_to_remove)
w = (word for word in line.split() if word in words)
counter.update(w)
print(counter)
我还举了一个例子来说明我对预处理的意思:在计数之前将被删除。在您的代码中,文件在第一行被消耗(耗尽),因此下一行不会返回任何要计数的内容:第一行
file.read()
读取文件的全部内容并将其作为字符串返回。第二个文件.read()
没有什么可读的,只返回一个空字符串'
——第三个文件.read()
这是一个可以满足您需要的版本:
from collections import Counter
counter = Counter()
with open('my_output', 'r') as file:
for line in file:
counter.update(line.split())
print(counter)
您可能需要进行一些预处理(以去除特殊字符和、
和
以及其他字符)
在python标准库中,对于这类事情非常有用
请注意,这样您只需在文件上迭代一次,而不必在任何时候将整个文件存储在内存中
如果您只想跟踪某些单词,您可以只选择它们,而不是将整行内容传递给计数器:
from collections import Counter
import string
counter = Counter()
words = ('wordA', 'wordB', 'wordC')
chars_to_remove = str.maketrans('', '', string.punctuation)
with open('my_output', 'r') as file:
for line in file:
line = line.translate(chars_to_remove)
w = (word for word in line.split() if word in words)
counter.update(w)
print(counter)
我还提供了一个预处理示例:在计数前将被删除。你能试试这个吗:
file = open('my_output', 'r')
splitFile = file.read().split()
lst = ['wordA','wordB','wordC']
for wrd in lst:
print(wrd, splitFile.count(wrd))
你能试试这个吗
file = open('my_output', 'r')
splitFile = file.read().split()
lst = ['wordA','wordB','wordC']
for wrd in lst:
print(wrd, splitFile.count(wrd))
我认为,与其不断地读取和拆分文件,不如这样做,这段代码会工作得更好:[这样可以找到文件中任意数量单词的词频]
file=open('my_output' , 'r')
s=file.read()
s=s.split()
w=set(s)
tf={}
for i in s:
tf[i]=s.count(i)
print(tf)
我认为,与其不断地读取和拆分文件,不如这样做,这段代码会工作得更好:[这样可以找到文件中任意数量单词的词频]
file=open('my_output' , 'r')
s=file.read()
s=s.split()
w=set(s)
tf={}
for i in s:
tf[i]=s.count(i)
print(tf)
使用集合的简短解决方案。计数器对象:
import collections
with open('my_output', 'r') as f:
wordnames = ('wordA', 'wordB', 'wordC')
counts = (i for i in collections.Counter(f.read().split()).items() if i[0] in wordnames)
for c in counts:
print(c[0], c[1])
对于以下示例文本行:
'wordA some dfasd asdasdword B wordA sdfsd sdasdasdddasd wordB wordC wordC sdfsdfsdf wordA'
我们将获得以下输出:
wordB 1
wordC 2
wordA 3
使用集合的简短解决方案。计数器对象:
import collections
with open('my_output', 'r') as f:
wordnames = ('wordA', 'wordB', 'wordC')
counts = (i for i in collections.Counter(f.read().split()).items() if i[0] in wordnames)
for c in counts:
print(c[0], c[1])
对于以下示例文本行:
'wordA some dfasd asdasdword B wordA sdfsd sdasdasdddasd wordB wordC wordC sdfsdfsdf wordA'
我们将获得以下输出:
wordB 1
wordC 2
wordA 3
请考虑用一些解释来扩充你的答案,而不是只为将来的代码。readers@etov,好建议。包括每个步骤的步骤,请考虑用一些解释来扩充你的答案,而不是只为将来的代码。readers@etov,好建议。包括每个步骤的步骤