文件中的python只读整数

文件中的python只读整数,python,Python,我想不出读取此文件的方法,只有整数: 34 -1 2 48 +0 ++2 +1 2.4 1000 -0 three -1 该函数应返回: [34, -1, 0, 1, -1] 如果一个数字有+或-是有效的。但是如果它有++或者任何字母都没有 如果它有空格(例如248)则无效 如果大于999,则无效 我只是被困在这里: my_list = [] with open('test.txt') as f: lines = f.readlines() for line in

我想不出读取此文件的方法,只有整数:

34
-1
2 48
  +0
++2
+1
 2.4
1000
-0
three
-1  
该函数应返回:

[34, -1, 0, 1, -1]
如果一个数字有
+
-
是有效的。但是如果它有
++
或者任何字母都没有

如果它有空格(例如
248
)则无效

如果大于999,则无效

我只是被困在这里:

my_list = []
with open('test.txt') as f:
    lines = f.readlines()
    for line in lines:
        my_list.append(line.strip())
我试着用
translate
把它变成一个字符串并使用标点符号,但我不确定它是否变得更复杂


另外,我不确定是否使用正则表达式。我尝试了一个简单的正则表达式,但我没有使用它的经验。

我认为正则表达式是适合你的方法。您可以通过这样的方式实现您想要的:
[-+]?\d*
它查找+或-,问号表示可选,然后是任意数字。 为您的案例找到合适的正则表达式的简单方法是。您可以直接看到什么与您的正则表达式匹配,并向您解释。在python中,re模块()可以使用正则表达式


希望这对您有所帮助。

我认为正则表达式是您的最佳选择。您可以通过这样的方式实现您想要的:
[-+]?\d*
它查找+或-,问号表示可选,然后是任意数字。 为您的案例找到合适的正则表达式的简单方法是。您可以直接看到什么与您的正则表达式匹配,并向您解释。在python中,re模块()可以使用正则表达式


希望这对您有所帮助。

您可以使用
int()
将字符串转换为整数。如果字符串不是整数,它将抛出
ValueError
。所以试试这个:

my_list = []
with open('test.txt') as f:
    for line in f:
        try:
            n = int(line)
            if n > 999 or line.strip() == '-0': continue #filtering numbers >999 and strings with '-0'
            my_list.append(n)
        except ValueError:
            pass

print(my_list)

输出:
[34,-1,0,1,-1]
您可以使用
int()
将字符串转换为整数。如果字符串不是整数,它将抛出
ValueError
。所以试试这个:

my_list = []
with open('test.txt') as f:
    for line in f:
        try:
            n = int(line)
            if n > 999 or line.strip() == '-0': continue #filtering numbers >999 and strings with '-0'
            my_list.append(n)
        except ValueError:
            pass

print(my_list)

输出:
[34,-1,0,1,-1]

如果您想手动执行(请注意,
regex
解决方案或调用
int
可能更合适,但这些已在其他答案中介绍),那么您也可以自己执行每个检查:

import string

characters_and_whitspaces = set(string.ascii_letters + ' .')

mylist = []

for line in lines:
    # remove leading and trailing whitespaces
    val = line.strip()

    # Check if valid (!= -0)
    if val == '-0':
        continue
    # Must not start with ++, +-, ....
    if val.startswith(('++', '+-', '-+', '--')):
        continue
    # Must not contain letters or whitespaces or a dot
    if characters_and_whitspaces.intersection(val):
        continue
    # Must only contain 3 or less digits (<= 999) or 4 if it starts with + or -
    if val.startswith(('+', '-')):
        if len(val) >= 5):
            continue
    elif len(val) >= 4:
        continue

    # Remove leading "+"
    val = val.lstrip('+')

    mylist.append(val)
导入字符串
字符_和_whitspaces=set(string.ascii_字母+'。)
mylist=[]
对于行中的行:
#删除前导空格和尾随空格
val=line.strip()
#检查是否有效(!=-0)
如果val='-0':
持续
#不能以++、+-、….开头。。。。
如果val.startswith((“++”、“+-”、“-+”、“-+”、“-”):
持续
#不得包含字母、空格或点
如果字符_和_为空格。交点(val):
持续
#只能包含3位或更少的数字(=5):
持续
elif len(val)>=4:
持续
#删除前导“+”
val=val.lstrip(“+”)
mylist.append(val)

如果您想手动执行(请注意,
regex
解决方案或调用
int
可能更合适,但这些已在其他答案中介绍),那么您也可以自己执行每个检查:

import string

characters_and_whitspaces = set(string.ascii_letters + ' .')

mylist = []

for line in lines:
    # remove leading and trailing whitespaces
    val = line.strip()

    # Check if valid (!= -0)
    if val == '-0':
        continue
    # Must not start with ++, +-, ....
    if val.startswith(('++', '+-', '-+', '--')):
        continue
    # Must not contain letters or whitespaces or a dot
    if characters_and_whitspaces.intersection(val):
        continue
    # Must only contain 3 or less digits (<= 999) or 4 if it starts with + or -
    if val.startswith(('+', '-')):
        if len(val) >= 5):
            continue
    elif len(val) >= 4:
        continue

    # Remove leading "+"
    val = val.lstrip('+')

    mylist.append(val)
导入字符串
字符_和_whitspaces=set(string.ascii_字母+'。)
mylist=[]
对于行中的行:
#删除前导空格和尾随空格
val=line.strip()
#检查是否有效(!=-0)
如果val='-0':
持续
#不能以++、+-、….开头。。。。
如果val.startswith((“++”、“+-”、“-+”、“-+”、“-”):
持续
#不得包含字母、空格或点
如果字符_和_为空格。交点(val):
持续
#只能包含3位或更少的数字(=5):
持续
elif len(val)>=4:
持续
#删除前导“+”
val=val.lstrip(“+”)
mylist.append(val)

如果要通过正则表达式执行此操作:

import re
exp = re.compile(r'^[\+,\-]?[0-9]{1,3}$')

my_list = []
with open('input.txt') as f:
    lines = f.readlines()
    for line in lines:
        if re.match(exp, line.strip()):
            my_list.append(int(line.strip()))
让我们解释一下正则表达式

^[\+,\-]?
-
^
表示表达式必须以下一个限定符开头,这是两个字符的列表
\+
\-
。我们需要那里的逃逸斜线来实际放入特殊的字符。最后一个
使前面的参数成为可选参数(因此字符串可以以+或-,或无开头)


[0-9]{1,3}$
-
[0-9]
指定作为数字的字符集
{1,3}
指定它们至少出现一次,或最多出现3次(因此,如果希望通过正则表达式执行此操作,请满足

import re
exp = re.compile(r'^[\+,\-]?[0-9]{1,3}$')

my_list = []
with open('input.txt') as f:
    lines = f.readlines()
    for line in lines:
        if re.match(exp, line.strip()):
            my_list.append(int(line.strip()))
让我们解释一下正则表达式

^[\+,\-]?
-
^
表示表达式必须以下一个限定符开头,这是一个由两个字符组成的列表
\+
\-
。我们需要在其中转义斜杠来实际放入特殊字符。最后的
使前面的参数成为可选参数(因此字符串可以以+或-,或无开头)


[0-9]{1,3}$
-
[0-9]
指定作为数字的字符集。
{1,3}
指定它们应至少出现一次,或最多出现3次(因此满足您的
以下是regexp解决方案:

import re

rgx = re.compile(r'^\s*[-+]?\s*(?:0|0*\d{1,3})\s*$', re.M)

with open('test.txt') as f:
    my_list = [int(match) for match in rgx.findall(f.read())]
输出:

[34, -1, 0, 1, 0, -1]

下面是一个regexp解决方案:

import re

rgx = re.compile(r'^\s*[-+]?\s*(?:0|0*\d{1,3})\s*$', re.M)

with open('test.txt') as f:
    my_list = [int(match) for match in rgx.findall(f.read())]
输出:

[34, -1, 0, 1, 0, -1]

与所有正则表达式一样,最好将其设置为原始字符串:
r“…”
。在Python 3.6中,如果不将其指定为原始字符串,则不推荐使用
\+
。但这是一个非常好的解决方案和解释(+1)感谢@MSeifert,为了完整性,我们编辑了答案,将其设置为原始字符串。与所有正则表达式一样,最好将其设置为原始字符串:
r“…”
。在Python 3.6中,
\+
如果不指定为原始字符串,则不推荐使用。但这是一个非常好的解决方案和解释(+1)感谢@MSeifert,为了完整性,我们编辑了答案,使其成为原始字符串。从问题中可以看出,
-0
也应该无效。在这种情况下,请将if语句替换为:
if n>999或(n==0且“-”在同一行):继续
谢谢。我赞成