从文本文件lua解析科学数字

时间:2013-08-01 19:06:49

标签: parsing text lua scientific-notation

有人知道如何用lua解析文本文件中的科学数字吗?

示例textfile:

0.2 0.5 0.15 5.32E-05 0.5

0.2 6.32E-08 0.5

我在相关主题中找到了如何获取数字(下图),但是使用该代码,它将'5.32E-05'视为:5.32和-0.5。

local tt = {}
for line in io.lines(filename) do
local t = {}
for num in line:gmatch'[-.%d]+' do
  table.insert(t, tonumber(num))
end
if #t > 0 then
  table.insert(tt, t)
end
end

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

如果您确定要阅读数字,可以使用*n阅读模式。

如果您在示例代码中逐行阅读文件,请使用%S+提取行中的字词,并将其转换为tonumber的数字。

底线:将重度解析留给tonumber

相关问题