def ints(filename):
a = []
f = open(filename, "r")
lines = f.readlines()
f.close()
for line in lines:
numbers = line.split()
for number in numbers:
a.append(int(number))
return a
到目前为止,这是我的函数,我希望能够读取包含整数和字符(如“x”和“b”等)的文件,并返回仅包含整数的列表。目前该函数只能处理包含整数的文件。
如何修改此字符以排除字符或字母?
答案 0 :(得分:4)
这是我对您的代码的编辑,它按预期执行。
def ints(filename):
"""A function that takes the filename of a file as an input arguement, computs and returns a list of
integers of all the numbers in the file."""
a = []
f = open(filename, "r")
lines = f.readlines()
f.close()
for line in lines:
for character in line:
try:
a.append(int(character))
except ValueError:
pass
return a
答案 1 :(得分:2)
regex
可以在这里提供帮助:
一个简单的例子:
In [22]: import re
In [23]: strs="121 some 34useless text 56"
In [24]: map(int,re.findall("\d+",strs))
Out[24]: [121, 34, 56]
# or this If you want the individual digits:
In [40]: map(int,re.findall("\d",strs))
Out[40]: [1, 2, 1, 3, 4, 5, 6]
对于您的代码,这应该有效:
for line in lines:
numbers = map(int,re.findall("\d+",line))
a.extend(numbers)
答案 2 :(得分:1)
我只是测试字符是否为数字:
sample_string = "Test4. 2325This string3"
a_list = []
for x in sample_string:
if x.isdigit():
a_list.append(x)
答案 3 :(得分:0)
for number in numbers:
try:
a.append(int(number))
except ValueError:
pass
答案 4 :(得分:0)
尝试/捕获可能会有所帮助:
for thing in line.split():
i_thing = None
try:
i_thing = int(thing)
except ValueError:
pass
s_thing = None
try:
s_thing = str(thing)
except:
raise Exception("OH NOES!")
这很丑陋,但我没有找到更好的方法去做你想做的事情。
答案 5 :(得分:0)
使用更现代的Python习语:
def ints(filename):
with open(filename, "r") as f:
for line in f:
for number in line.split():
try:
yield int(number)
except ValueError:
pass
a = list(ints("testdata.txt"))
print(a)
基本上,尝试转换为int,如果字符串不是十进制数,则会引发ValueError。抓住它并忽略它并继续。
答案 6 :(得分:0)
如何使用string.translate等来替换所有带空格的非数字,然后利用split()和map()的强大功能。
虽然它有点模糊,我的默认响应是只使用re模块,因为你可以用正则表达式做更多的事情,他们值得学习。< / p>
In [119]: import string
In [120]: allchars = string.maketrans('', '')
In [121]: delchars = allchars.translate(allchars, "0123456789")
In [122]: emptychars = string.maketrans(delchars, ' ' * len(delchars))
In [123]: "Welcome home 1234 56 ol".translate(emptychars)
Out[123]: ' 1234 56 '
In [124]: "Welcome home 1234 56 ol".translate(emptychars).split()
Out[124]: ['1234', '56']
In [125]: map(int, "Welcome home 1234 56 ol".translate(emptychars).split())
Out[125]: [1234, 56]
答案 7 :(得分:0)
这是未经测试的sudo-code,但应该有效。另外,我能给出的最佳建议是观看David Beazley的系统程序员生成器技巧和掌握Python 3 I / O会话。他们在学习python方面有很大的帮助。
这只是一个简单的生成器,用于获取文件的每一行并在之后关闭文件。
def getLine(fileName):
file = open(fileName, "r")
for line in file.readLines():
yield line
file.close()
def getNumbers(line):
// I'm lazy and stole this one from Keith
for number in line.split():
try:
yield int(number)
except ValueError:
pass
def generatorChain(fileName):
// I'm a little iffy on the syntax here, but shouldn't be to hard with a bit of googling
fileGen = getLine(fileName);
yield getNumber( fileGen.next() ).next()
def listCompressionMagic():
return [x for x in generatorChain("foo.txt") ]