![](/img/trans.png)
[英]Count number of lines that contain numbers after each string in a text file
[英]Python count the number of lines that contain numbers in a text file
假设我有这个文本文件:
some text
120 130
1847 1853
other text
207 220
text
306 350
some other text
400 435
900 121
125 369
我想计算每个字符串后面包含数字的行数,所以我的 output 将如下所示:
2
1
1
3
这是我尝试过的:
m=0
with open('some_txt_file.txt','r') as f:
for line in f:
if line.replace(" ","").isdigit():
m+=1
else:
m=0
但这给了我一个错误的结果。 我该如何编码?
您唯一缺少的是每行以'\n'
结尾的事实,这破坏了.isdigit()
测试。 让我们使用相同的代码,但只是.strip()
那行结束:
m = 0
with open('some_txt_file.txt','r') as f:
for line in f:
if line.strip().replace(" ", "").isdigit():
m += 1
else:
if m: print(m)
m = 0
if m: print(m)
# prints:
# 2
# 1
# 1
# 3
尝试这个:
m=0
with open('some_txt_file.txt','r') as f:
first = True
for line in f:
if first and line.strip().replace(' ', '').isdigit():
continue
elif first:
first = False
continue
if line.strip().replace(' ', '').isdigit():
m+=1
else:
print(m)
m=0
print(m)
isdigit()
当前将失败,因为数字中有空格,因此最好检查行中的数字。 如果任何行后面没有任何数字,这也将起作用,这将在其他答案中失败,因为它们忽略 m==0
我尝试过这种方式。 仅当第一行始终为非数字时,此方法才能正常工作:
text = """some text
120 130
1847 1853
other text
207 220
text
306 350
some other text
400 435
900 121
125 369"""
lines = text.split("\n")
c = 0
for line in lines[1:]:
if line[0].isdigit():
c+=1
else:
print(c)
c=0
print(c)
# 2
# 1
# 1
# 3
我不太确定我会说什么,我无法测试下面的代码,但这是我认为仍然存在的问题
寻找 readline() 函数加上,行总是一个字符串,每次你要测试它时总是使用“else”条件。 尝试投线之前。
count = 0
with open('some_txt_file.txt','r') as f:
line = f.readline()
while line:
try:
int(line)
count++
line = file.readline()
except:
line = file.readline()
这会起作用
import re
res = re.findall("[0-9 \n]+",a,re.DOTALL|re.IGNORECASE)
for each in res:
if each.strip()
print(len([i for i in each.split("\n") if i]))
性能方面,在这里使用生成器和正则表达式匹配感觉更好。 特别是如果您要解析大数据集。 我将运行一些基准测试。 这是我的解决方案:
pattern = '^[\d\s]*\d+[\d\s]*$'
### The pattern means:
# ^ # start of line
# [\d\s]* # zero or more digits or whitespaces
# \d+ # required at least one digit
# [\d\s]* # again zero or more digits or whitespaces
# $ # end of line
with open('some_txt_file.txt') as f:
result = sum([1 for line in f if re.match(pattern, line)])
print(result)
生成器将为匹配模式的每一行添加 1 进行计数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.