我在尝试计算python列表中的空格时遇到问题。
这是我的代码
Data = ''
index = 0
num_words = 0
# Open a file for reading.
infile = open('article.txt', 'r')
# Read the contents of the file into a list.
data = infile.readlines()
# Strip the \n from each element.
while index < len(data):
data[index] = data[index].rstrip('\n')
index += 1
for ch in data:
if ch.isspace():
num_words += 1
# Close the file.
infile.close()
# Print the contents of the list.
print(num_words)
article.txt的内容只是一个句子列表,因此该列表只是一个字符串列表,例如:
data = ['this is sentence one.', 'this is sentence two.' , 'this is sentence three.' , 'this is sentence four.' , 'this is sentence five.' , 'this is sentence six.' ]
我想我知道问题出在哪里,因为我做了:
print(ch)
这将导致“假”被打印6次。我在想这是因为for循环正在搜索整个字符串是否为空格,而不是检查字符串内部的空格。
我知道我可以做到:
data = infile.read()
但是我需要列表中的每一行。有什么我可以更改的,以便for循环在列表中的每个字符串中搜索空格吗?还是我不走运?
答案 0 :(得分:0)
Python在字符串上有一个方便的方法,称为str.split
。如果不传递任何参数,它将在空白处分割。如果您对结果列表中的项目进行计数,您将拥有字数。
处理多个空格:
>>> line = "this is some string."
>>> len(line.split())
4
处理空行:
>>> line = " "
>>> len(line.split())
0
处理前后的多余空间
>>> line = " space before and after. "
>>> len(line.split())
4
以下是一些示例代码:
lines = 0
words = 0
with open('yourfile', 'rt') as yourfile:
for line in yourfile:
lines += 1
words += len(line.split())