2016-03-01 85 views
1

所以我碰上我已经开了一个.dat文件,并试图从中提取数的情况:只保留INT在Python

self.text= (open("circles.dat", "r")).readlines() 
print (self.text) 

输出:

['200 200 100\n', '75\t200\t15\n', ' 325\t200\t15\n', '\n', '\t200\t\t75 10\n', '200 325 10\n'] 

有没有一种方法可以只提取int数字而不包含任何其他内容。 编辑: 的eval()不能用于 我所要的输出是这样的:

[200,200,100,75,200,15,325,200,15,200,75,10,200,325,10] 
+2

你应该读行线和解析这些线路。 – RafaelC

+0

你想要一个长整数列表,还是列表列表? – zondo

+0

用于删除\ t和\ n的东西,使用eval() – JustDucky

回答

2
>>> import re 
>>> num_list = map(int, re.findall(r'\d+', open("circles.dat", "r").read())) 
[200, 200, 100, 75, 200, 15, 325, 200, 15, 200, 75, 10, 200, 325, 10] 

使用.read()代替.readlines()为read()返回整个文件的内容作为一个字符串(可以与正则表达式一起使用),而不像readlines()返回字符串列表。

获取数字列表(以字符串形式)后,使用map()函数将列表类型转换为int类型。

步骤解释

>>> import re 
>>> file_content = open("circles.dat", "r").read() # Read file as single string 
>>> num_list = re.findall(r'\d+', file_content) # Fetch all numbers from string 
>>> num_list 
['200', '200', '100', '75', '200', '15', '325', '200', '15', '200', '75', '10', '200', '325', '10'] 
>>> map(int, num_list) # Typecast list of str to list of int 
[200, 200, 100, 75, 200, 15, 325, 200, 15, 200, 75, 10, 200, 325, 10] 
3

假设你拥有所有int S和它们(如空格或制表符)之间只是空格,那么你可以使用一个简单的列表理解与str.split()

>>> with open("circles.dat", "r") as f: 
...  d = [int(a) for l in f for a in l.split()] 
>>> d 
[200, 200, 100, 75, 200, 15, 325, 200, 15, 200, 75, 10, 200, 325, 10] 
0

无任何模块的解决方案

>>> x = ['200 200 100\n', '75\t200\t15\n', ' 325\t200\t15\n', '\n', '\t200\t\t75 10\n', '200 325 10\n'] 
>>> 
>>> y = "".join(x) # join together 
>>> print y 
'200 200 100\n75\t200\t15\n 325\t200\t15\n\n\t200\t\t75 10\n200 325 10\n' 
>>> 
>>> z = y.replace("\t", " ").replace("\n", " ") # replace tabs and new lines 
>>> print z 
'200 200 100 75 200 15 325 200 15 200 75 10 200 325 10 ' 
>>> 
>>> z = z.split() # removes all whitespace by default 
>>> print z 
['200', '200', '100', '75', '200', '15', '325', '200', '15', '200', '75', '10', '200', '325', '10'] 
>>> 
>>> res = map(int, z) # convert all to integers 
>>> print res 
[200, 200, 100, 75, 200, 15, 325, 200, 15, 200, 75, 10, 200, 325, 10] 

解决方案作为一个丑陋的一行(仅为80个字符!)

res = map(int, "".join(self.text).replace("\t", " ").replace("\n", " ").split()) 
1
>>> self.text = (open("circles.dat", "r")).readlines() 
>>> print self.text 
['200 200 100\n', '75\t200\t15\n', ' 325\t200\t15\n', '\n', '\t200\t\t75 10\n', '200 325 10\n'] 
>>> 
>>> ans = map(lambda s: s.rstrip().replace("\t", " "), self.text) 
>>> ans = " ".join(ans) 
>>> ans = ans.split() 
>>> 
>>> final_ans = [int(a) for a in ans] 
>>> final_ans = map(int, ans) # alternative 
>>> print final_ans 
[200, 200, 100, 75, 200, 15, 325, 200, 15, 200, 75, 10, 200, 325, 10]