[英]How to read a file and print lines with only a certain amount of digits
input_file = input("Open what file:")
try:
input_file = open(input_file)
for line_str in input_file:
if input_file == 4 and line_str.isdigit():
print(line_str)
except IOError:
print("The input file doesn't exist.")
sys.exit(1)
input_file.close
In my text file I had these numbers: 在我的文本文件中,我有以下数字:
174862
2000
2400
9996
12
55
How do I make so that only the numbers with at least 4 digits are printed out? 我该如何做才能只打印至少4位的数字?
You need to strip the new line char which is appended to line_str. 您需要删除添加到line_str的新行char。
Try something like this: 尝试这样的事情:
input_file = "file.txt"
try:
input_file = open(input_file)
for line_str in input_file:
line_str = line_str.strip()
if len(line_str) == 4 and line_str.isdigit():
print(line_str)
except IOError:
print("The input file doesn't exist.")
sys.exit(1)
input_file.close
You can also use a regex to see if you have a set of 4 digits. 您还可以使用正则表达式查看是否有一组4位数字。 I can add how that can be accomplished if you want me to. 如果您需要我可以补充说明。
EDIT: To match using a regex, you can use the below 编辑:要使用正则表达式进行匹配,可以使用以下内容
import re
line_str = line_str.strip()
if re.match(r'^[0-9]{4}$', line_str):
print "RE " + line_str
re is pythons regular expression module. re是pythons正则表达式模块。 re.match will return a None object if there is no match. 如果没有匹配项,则re.match将返回None对象。 Hence you can use it directly in the if condition. 因此,您可以在if条件中直接使用它。 r'^[0-9]{4}$' - is basically your regular expression. r'^ [0-9] {4} $'-基本上是您的正则表达式。 '^' denotes beginning, '$' denotes end. “ ^”表示开始,“ $”表示结束。 [0-9] is the number range, and {} denotes count. [0-9]是数字范围,{}表示计数。
Hope it clears things up. 希望它能清除一切。
Instead of comparing input_file == 4
(which will never be true, since input_file
is a file object, not an integer), you want to compare the len
of your line_str
with 4. Try: 而不是比较input_file == 4
(永远不会为真,因为input_file
是文件对象,而不是整数),而是想将line_str
的len
与4进行比较。请尝试:
for line_str in input_file:
if len(line_str) >= 4 and line_str.isdigit():
print(line_str)
This will print only lines that are at least four characters long and consist only of digits. 这将仅打印至少四个字符长且仅由数字组成的行。
You probably should also be using a with
statement to handle the closing of the file, but that's a minor issue (it will probably work just find without it). 您可能还应该使用with
语句来处理文件的关闭,但这是一个小问题(如果没有它,它可能会起作用)。
import re
try:
input_file = open("input.txt",'r')
x=input_file.read()
print re.findall(r"(\d{4,})",x)
except IOError:
print("The input file doesn't exist.")
sys.exit(1)
input_file.close
Try this.This will give all numbers of greater than length 4 . 试试这个。这将给出大于长度4的所有数字。
This doesnt loop through file as well. 这也不会遍历文件。
You can use the len() function to check the length of the number you are reading. 您可以使用len()函数检查正在读取的数字的长度。
I have modified your program. 我已经修改了您的程序。
import sys
input_file = input("Open what file:")
try:
with open(input_file) as in_file:
for line_str in in_file:
if len(line_str.strip()) >= 4 and line_str.strip().isdigit():
print(line_str.strip())
except IOError:
print("The input file doesn't exist.")
sys.exit(1)
Also you don't require file close when you use with
. 另外,当您with
使用时,您不需要关闭文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.