【问题标题】:parse scientific number from text file lua从文本文件 lua 中解析科学数字
【发布时间】:2013-08-01 19:06:48
【问题描述】:

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

示例文本文件:

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

有人可以帮我吗?

【问题讨论】:

    标签: parsing text lua scientific-notation


    【解决方案1】:

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

    如果您像示例代码中那样逐行读取文件,则使用%S+ 提取该行中的单词,然后使用tonumber 将它们转换为数字。

    底线:将繁重的解析留给tonumber

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      相关资源
      最近更新 更多