我有一点粗略的时间来阐述如何使用Python计算文本文件中的某些元素。我进入Python几个月,我熟悉以下功能;
到目前为止,这是我的代码:
fname = "feed.txt"
fname = open('feed.txt', 'r')
num_lines = 0
num_words = 0
num_chars = 0
for line in feed:
lines = line.split('\n')
此时我不知道接下来该做什么。我觉得接近它的最合乎逻辑的方法是首先计算行数,计算每行中的单词,然后计算每个单词中的字符数。但我遇到的一个问题是尝试同时执行所有必要的功能,而不必重新打开文件来单独执行每个功能。
答案 0 :(得分:22)
试试这个:
fname = "feed.txt"
num_lines = 0
num_words = 0
num_chars = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_lines += 1
num_words += len(words)
num_chars += len(line)
返回您的代码:
fname = "feed.txt"
fname = open('feed.txt', 'r')
这有什么意义? fname
首先是字符串,然后是文件对象。你并没有真正使用第一行中定义的字符串,你应该只使用一个变量:字符串或文件对象。
for line in feed:
lines = line.split('\n')
line
是文件中的一行。对split('\n')
它没有意义。
答案 1 :(得分:3)
可能有用的功能:
open("file").read()
一次性读取整个文件的内容'string'.splitlines()
将线条彼此分开(并丢弃空行)通过使用len()和那些功能,你可以完成你正在做的事情。
答案 2 :(得分:2)
fname = "feed.txt"
feed = open(fname, 'r')
num_lines = len(feed.splitlines())
num_words = 0
num_chars = 0
for line in lines:
num_words += len(line.split())
答案 3 :(得分:1)
我喜欢的方式之一就是这个,但可能适用于小文件
with open(fileName,'r') as content_file:
content = content_file.read()
lineCount = len(re.split("\n",content))
words = re.split("\W+",content.lower())
要计算单词,有两种方法,如果你不关心重复,你可以做到
words_count = len(words)
如果你想要你可以做的每个单词的计数
import collections
words_count = collections.Counter(words) #Count the occurrence of each word
答案 4 :(得分:1)
file__IO = input('\nEnter file name here to analize with path:: ')
with open(file__IO, 'r') as f:
data = f.read()
line = data.splitlines()
words = data.split()
spaces = data.split(" ")
charc = (len(data) - len(spaces))
print('\n Line number ::', len(line), '\n Words number ::', len(words), '\n Spaces ::', len(spaces), '\n Charecters ::', (len(data)-len(spaces)))
我尝试了此代码,它可以按预期工作。 提前致谢。