为什么我不能将 .txt 文件存储为表格?

我在树莓派上运行基于 python 的 Web 服务器,通过爬取 Roblox 上的交易货币汇率。如果你不知道我在说什么,你只需要知道我正在收集某个网页上不断变化的数字。我想将这些收集到的信息导入我的 Roblox 游戏中,以便我可以对其进行图形化显示(我已经做好了绘图器)。

以下是我导入它的方法:

bux = game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableRobux.txt")
bux = {bux}
tix = game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableTickets.txt")
tix = {tix}

这给我返回了一个 404 错误响应。如果我从我的计算机(在同一网络上)访问 Web 服务器,它也会返回 404 错误响应。我知道我正在正确地进行端口转发,因为下面的 Lua 代码行确实起作用。

print(game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableRobux.txt"))

我需要将 Robux 和 Ticket 汇率存储在一个 table 中。转到带有汇率数据的 URL 之一,你会发现它已经为 Rbx.Lua table 格式化好了,只需要添加大括号就可以。如何将我的数据转换为 table 呢?

点赞
用户270483
用户270483

你不能像那样将字符串转换为表格,你需要将组件按照分隔符(逗号)拆分为表格。参考这里关于在 Lua 中拆分字符串的页面。我建议去掉空格,仅在数据项之间用逗号。

以下是需要的示例。explode 函数来自我发布的链接。我没有进行测试。

function explode(d,p)
  local t, ll
  t={}
  ll=0
  if(#p == 1) then return {p} end
    while true do
      l=string.find(p,d,ll,true) -- 找到字符串中下一个 d
      if l~=nil then -- 如果找到了
        table.insert(t, string.sub(p,ll,l-1)) -- 将其保存到我们的数组中
        ll=l+1 -- 将搜索下一个位置的指针移到找到的后面
      else
        table.insert(t, string.sub(p,ll)) -- 将剩余部分保存到我们的数组中
        break -- 到达结尾,则返回,与 Lua 手册相符。
      end
    end
  return t
end

bux = game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableRobux.txt")
bux = explode(",",bux)
tix = game.HttpService:GetAsync("http://tcserver.raspctl.com:5000/AvailableTickets.txt")
tix = explode(",",tix)
2015-03-15 22:16:35