读取文件,删除文本字段,保留数字文件
问题描述:
我想写一个小小的python脚本来绘制一些.dat文件。为此,我需要首先处理文件。 .dat文件如下所示:读取文件,删除文本字段,保留数字文件
(Real64
(numDims 1)
(size 513)
(data
[ 90.0282291905089 90.94377050431068 92.31708247501335 93.38521400778211 94.60593575951782 95.67406729228657 97.04737926298925 97.96292057679104 ...]
)
)
我想删除文本部分和“正常”括号。我只需要[...]之间的数据。
我想是这样的:
from Tkinter import Tk
from tkFileDialog import askopenfilename
# just a small GUI to get the file
Tk().withdraw()
filename = askopenfilename()
import numpy as np
with open(filename) as f:
temp = f.readlines(5) #this is the line in the .dat file
for i in range(len(temp)-1):
if type(temp[i]) == str:
del temp[i]
然而,这会导致产生一个“出界指数”。帮助将不胜感激。
答
我只需要数据在[......]
# treat the whole thing as a string
temp = '''(Real64
(numDims 1)
(size 513)
(data
[ 90.0282291905089 90.94377050431068 92.31708247501335 ]
)
)'''
# split() at open bracket; take everything right
# then split() at close bracket; take everything left
# strip() trailing/leading white space
number_string = temp.split('[')[1].split(']')[0].strip()
# convert to list of floats, because I expect you'll need to
number_list = [float(i) for i in number_string.split(' ')]
print number_string
print number_list
>>> 90.0282291905089 90.94377050431068 92.31708247501335
>>> [90.0282291905089, 90.94377050431068, 92.31708247501335]
+0
这工作得很好,谢谢! –
答
print re.findall("\[([0-9. ]+)\]",f.read())
这就是所谓的regular expression
和它说发现我所有的东西,是在方括号
\[ # literal left bracket
(# capture the stuff in here
[0-9. ] # accept 0-9 and . and space
+ # at least one ... probably more
) # end capture group
\] # literal close bracket
或者你可以使用类似pyparsing
inputdata = '''(Real64
(numDims 1)
(size 513)
(data
[ 90.0282291905089 90.94377050431068 92.31708247501335 93.38521400778211 94.60593575951782 95.67406729228657 97.04737926298925 97.96292057679104 ...]
)
)
'''
from pyparsing import OneOrMore, nestedExpr
data = OneOrMore(nestedExpr()).parseString(inputdata)
print "GOT:", data[0][-1][2:-1]
两者之间的数字时间和空间
+0
谢谢,这确实帮了我一些忙! –
你从哪里得到'.dat'文件?你可能有任何产生它给你另一种格式(如JSON)?如果不是,可以用逗号替换空格,并将其解析为JSON。 –
你是什么意思*“删除文本部分”*?清楚。向我们展示给定输入的预期输出。应该'(size 513)' - >'(513)'还是'513'或完全删除?你可以使用正则表达式来完成所有这些,但是你没有为我们指定你想要做什么。 – smci
您是否尝试过使用正则表达式? – chbchb55