我想处理这样的数据文件:
DateTime
并输出部分列。
到目前为止,我设法:
2015-02-23 190 170 131 14 8 9 130 85 102.0 12 68
2015-02-24 165 128 97 14 7 6 110 75 101.7 12 64
2015-02-25 160 123 129 11 5 7 130 85 101.3 12 68
2015-02-26 151 115 128 11 nan 7 120 80 100.9 12 64
2015-02-27 141 119 130 11 4 nan 130 85 101.6 12 68
2015-02-28 142 137 143 nan nan nan 120 80 101.2 12 64
这适用于一列:local infile = arg[1]
local outfile = arg[2]
local column = tonumber(arg[3])
local data = {}
local row = 0
local ofile
for line in io.lines(infile)
do
row = row + 1
data[row] = {}
local i = 0
for value in string.gmatch(line, "%S+") do
i = i + 1
data[row][i] = value
end
end
ofile = assert(io.open(outfile, "w"))
for i = 1,row do
ofile:write(data[i][column] .. "\n")
end
ofile:close()
lua column.lua test.dat new.dat 2
我希望190
165
160
151
141
142
在新文件中包含第1,2和4列。这可能吗?
答案 0 :(得分:3)
您可以使用以下函数提取列列表:
function cols(t, colnums, prefix)
-- get the number of the first column and the rest of the numbers
-- it splits 1,2,3 into 1 and 2,3
local col, rest = colnums:match("^(%d+)[,%s]*(.*)")
-- if nothing is provided return current `prefix` value (may be `nil`)
if not col then return prefix end
-- convert the string with the column number into number
-- this is needed because t[1] and t['1'] references different values
local val = t[tonumber(col)]
-- call the same function recursively, but using the rest of the columns
-- this also concatenates the prefix (if any) with the current value
return cols(t, rest, prefix and prefix.."\t"..val or val)
end
现在可以使用ofile:write(data[i][column] .. "\n")
代替ofile:write(cols(data[i], arg[3]) .. "\n")
而不是<img>
。由于它在每一行上都进行了解析,因此对于大量行来说效率可能不高,所以如果你的情况如此,那么你可能需要在脚本的开头解析一次。 / p>