Lua: how to read .INI (text format) file only even lines and keeping only the right-side of a line after specific character

huangapple go评论48阅读模式
英文:

Lua: how to read .INI (text format) file only even lines and keeping only the right-side of a line after specific character

问题

我不是LUA程序员,但有时我必须处理它。

请看我的问题:

我有一个.INI文件,它是一个简单的文本格式文件,其中每个设置都以以下方式的两行书写(下面是使用的格式):

[machine_readable_plugin_name]
(plugin_name.maker.tld),flag=0|Human Readable Plugin Name (Maker)

文件包含数百对类似上面的行。

对于每一对行,我需要:
避免包含"[]"的所有第一行,我不需要它们的内容
从第二行开始,只获取竖线字符"|"后面的内容

print(string.match(s, "|(.*)"))

返回一个空字符串。
"s"包含我正在读取的文件行,我通过一个"FOR END"循环来读取这行代码。
通过使用print(s),我可以打印出所有的行。

我需要的是,对于每一对行,print()应该返回

Human Readable Plugin Name (Maker)

所以,如果.INI包含20行,我需要获取10次

Human Readable Plugin 1 Name (Maker 1)
Human Readable Plugin 2 Name (Maker 2)
Human Readable Plugin 3 Name (Maker 3)
...
...
Human Readable Plugin 10 Name (Maker 10)

注意:.INI文件不是由我创建的。它是与应用程序一起的,我不能修改它的格式。就是这样。

编辑1

请看你在评论中友好地提出的代码:

local path_ = 'K:/APP_NAME/settings-win64.ini'
local handle_ = io.open(path_, "r")

for _ in handle_:lines() do
    s = handle_:read("*l")
    print(s) -- 这个输出是正常的。所有行都被打印出来了。

    if _ == nil then 
        handle_:close() 
    else

        if s ~= nil then 
            human_Name = string.match(s, "|(.+)$")
            print("TEST") -- 这个按预期打印出来。
            print(s) -- 这个按预期打印出来。
            print(human_Name) -- 这个是空的。什么也没有打印。
        end
    end -- if 1
end -- for
英文:

I'm not a LUA programmer but sometimes I have to deal with.

Please here my issue:

I have an .INI file that's a text simple text formatted file, in which each setting is written in 2 lines in this way (here below the format used):

[machine_readable_plugin_name]
(plugin_name.maker.tld),flag=0|Human Readable Plugin Name (Maker)

The file contains hundreds of couple of lines like the one here above.

For each couple of lines, I need to:
Avoid all the first lines that are containing the "[]", I don't need their content
And from the second line, get only what is coming after the Pipe character "|"

print(string.match(s, "|(.*)"))

Returns me an empty string.
"s" contains the file's line I'm reading it by a "FOR END" cycle in which the above line code is in it.
By using print(s) I got all the lines printed out

What I need, is to get for each couple of line, the print() should return

Human Readable Plugin Name (Maker)

So, if the .INI contains 20 lines, I need to get 10 times

Human Readable Plugin 1 Name (Maker 1)
Human Readable Plugin 2 Name (Maker 2)
Human Readable Plugin 3 Name (Maker 3)
...
...
Human Readable Plugin 10 Name (Maker 10)

NOTE: the .INI file is not made by me. It's with the application and I can't modify it's format. It is what it is.

EDIT 1

Please here the code you kindly asked into the comments

local path_ = 'K:/APP_NAME/settings-win64.ini'
local handle_ = io.open(path_, "r")

for _ in handle_:lines() do
    s = handle_:read("*l")
    print(s) -- this output is ok. All lines are printed

    if _ == nil then 
        handle_:close() 
      else
     
        if s ~= nil then 
          human_Name = string.match(s, "|(.+)$")
          print("TEST") -- this one is printed out as expected.
          print(s) -- this one is printed out as expected.
          print(human_Name) -- this one is empty. Nothing is printed.
        end
    end -- if 1
  end -- for

答案1

得分: 0

以下是您要翻译的代码部分:

All anomalies were generated within this couple of lines:

for _ in handle_:lines() do
    s = handle_:read("l") <--- this line was the main issue generator

LUA was reading the file and it was reading it 2 times, creating all the issues I faced-out.

By removing the the second line, and modifying the code accordingly, everything started working fine. Also with the test file published by @Oka in the comments here below

local path_ = 'K:/APP_NAME/settings-win64.ini'
local handle_ = io.open(path_, "r")

for _ in handle_:lines() do
    --s = handle_:read("l")
    print(_) -- this output is ok. All lines are printed

    if _ == nil then 
        handle_:close() 
      else
     
        if s ~= nil then 
          human_Name = string.match(_, "|(.+)$")
          print("TEST") -- this one is printed out as expected.
          print(_) -- this one is printed out as expected.
          print(human_Name) -- this one is empty. Nothing is printed.
        end
    end -- if 1
  end -- for

请注意,我已经将 HTML 实体编码转换回原始字符,以便更容易阅读。

英文:

All anomalies were generated within this couple of lines:

for _ in handle_:lines() do
    s = handle_:read(&quot;*l&quot;) &lt;--- this line was the main issue generator

LUA was reading the file and it was reading it 2 times, creating all the issues I faced-out.

By removing the the second line, and modifying the code accordingly, everything started working fine. Also with the test file published by @Oka in the comments here below

local path_ = &#39;K:/APP_NAME/settings-win64.ini&#39;
local handle_ = io.open(path_, &quot;r&quot;)

for _ in handle_:lines() do
    --s = handle_:read(&quot;*l&quot;)
    print(_) -- this output is ok. All lines are printed

    if _ == nil then 
        handle_:close() 
      else
     
        if s ~= nil then 
          human_Name = string.match(_, &quot;|(.+)$&quot;)
          print(&quot;TEST&quot;) -- this one is printed out as expected.
          print(_) -- this one is printed out as expected.
          print(human_Name) -- this one is empty. Nothing is printed.
        end
    end -- if 1
  end -- for

huangapple
  • 本文由 发表于 2023年6月5日 17:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定