[英]Python: Counting words from a directory of txt files and writing word counts to a separate txt file
New to Python and I'm trying to count the words in a directory of text files and write the output to a separate text file. Python 的新手,我正在尝试计算文本文件目录中的单词并将 output 写入单独的文本文件。 However, I want to specify conditions.但是,我想指定条件。 So if word count is > 0 is would like to write the count and file path to one file and if the count is == 0. I would like to write the count and file path to a separate file.因此,如果字数 > 0,则想将计数和文件路径写入一个文件,如果计数 == 0。我想将计数和文件路径写入单独的文件。 Below is my code so far.以下是我到目前为止的代码。 I think I'm close, but I'm hung up on how to do the conditions and separate files.我想我已经很接近了,但我对如何处理条件和分离文件感到困惑。 Thanks.谢谢。
import sys
import os
from collections import Counter
import glob
stdoutOrigin=sys.stdout
sys.stdout = open("log.txt", "w")
def count_words_in_dir(dirpath, words, action=None):
for filepath in glob.iglob(os.path.join("path", '*.txt')):
with open(filepath) as f:
data = f.read()
for key,val in words.items():
#print("key is " + key + "\n")
ct = data.count(key)
words[key] = ct
if action:
action(filepath, words)
def print_summary(filepath, words):
for key,val in sorted(words.items()):
print(filepath)
if val > 0:
print('{0}:\t{1}'.format(
key,
val))
filepath = sys.argv[1]
keys = ["x", "y"]
words = dict.fromkeys(keys,0)
count_words_in_dir(filepath, words, action=print_summary)
sys.stdout.close()
sys.stdout=stdoutOrigin
I would strongly urge you to not repurpose stdout
for writing data to a file as part of the normal course of your program.我强烈建议您不要将stdout
用于将数据写入文件作为程序正常过程的一部分。 I also wonder how you can ever have a word "count < 0".我也想知道你怎么能有一个单词“count < 0”。 I assume you meant "count == 0".我假设您的意思是“count == 0”。
The main problem that your code has is in this line:您的代码存在的主要问题在于这一行:
for filepath in glob.iglob(os.path.join("path", '*.txt')):
The string constant "path"
I'm pretty sure doesn't belong there.我很确定字符串常量"path"
不属于那里。 I think you want filepath
there instead.我认为你想要那里的filepath
。 I would think that this problem would prevent your code from working at all.我认为这个问题会阻止你的代码工作。
Here's a version of your code where I fixed these issues and added the logic to write to two different output files based on the count:这是您的代码版本,我在其中修复了这些问题并添加了逻辑以根据计数写入两个不同的 output 文件:
import sys
import os
import glob
out1 = open("/tmp/so/seen.txt", "w")
out2 = open("/tmp/so/missing.txt", "w")
def count_words_in_dir(dirpath, words, action=None):
for filepath in glob.iglob(os.path.join(dirpath, '*.txt')):
with open(filepath) as f:
data = f.read()
for key, val in words.items():
# print("key is " + key + "\n")
ct = data.count(key)
words[key] = ct
if action:
action(filepath, words)
def print_summary(filepath, words):
for key, val in sorted(words.items()):
whichout = out1 if val > 0 else out2
print(filepath, file=whichout)
print('{0}: {1}'.format(key, val), file=whichout)
filepath = sys.argv[1]
keys = ["country", "friend", "turnip"]
words = dict.fromkeys(keys, 0)
count_words_in_dir(filepath, words, action=print_summary)
out1.close()
out2.close()
Result:结果:
file seen.txt:看到的文件.txt:
/Users/steve/tmp/so/dir/data2.txt
friend: 1
/Users/steve/tmp/so/dir/data.txt
country: 2
/Users/steve/tmp/so/dir/data.txt
friend: 1
file missing.txt:文件丢失.txt:
/Users/steve/tmp/so/dir/data2.txt
country: 0
/Users/steve/tmp/so/dir/data2.txt
turnip: 0
/Users/steve/tmp/so/dir/data.txt
turnip: 0
(excuse me for using some search words that were a bit more interesting than yours) (请原谅我使用了一些比你更有趣的搜索词)
Hello I hope I understood your question correctly, this code will count how many different words are in your file and depending on the conditions will do something you want.您好,我希望我正确理解了您的问题,此代码将计算您的文件中有多少个不同的单词,并根据条件执行您想要的操作。
import os
all_words = {}
def count(file_path):
with open(file_path, "r") as f:
# for better performance it is a good idea to go line by line through file
for line in f:
# singles out all the words, by splitting string around spaces
words = line.split(" ")
# and checks if word already exists in all_words dictionary...
for word in words:
try:
# ...if it does increment number of repetitions
all_words[word.replace(",", "").replace(".", "").lower()] += 1
except Exception:
# ...if it doesn't create it and give it number of repetitions 1
all_words[word.replace(",", "").replace(".", "").lower()] = 1
if __name__ == '__main__':
# for every text file in your current directory count how many words it has
for file in os.listdir("."):
if file.endswith(".txt"):
all_words = {}
count(file)
n = len(all_words)
# depending on the number of words do something
if n > 0:
with open("count1.txt", "a") as f:
f.write(file + "\n" + str(n) + "\n")
else:
with open("count2.txt", "a") as f:
f.write(file + "\n" + str(n) + "\n")
if you want to count same word multiple times you can add up all values from dictionary or you can eliminate try-except block and count every word there.如果您想多次计算同一个单词,您可以将字典中的所有值相加,或者您可以消除 try-except 块并计算那里的每个单词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.