How to read multiline values in inifile

北城以北 提交于 2019-12-08 00:57:19

问题


I am trying to use a multi line value as specified as list in ini file, I was able to read sections but not able read to multi line values from a given key.

I have tried to using list manner it completely failed.

# Ini file
[UNITS]
COMPARE_UNITS = [list
                    [11871000 118700]
                    [1198100 1198100]
                ]
[VARS]
OLD_REL = 4.3
NEW_REL = 4.5

I have tried to using string based format also i failed but i could able to read sections and first line of a given key value.

# Ini file
[UNITS]
COMPARE_UNITS = "
                    11871000 118700
                    1198100 1198100
                "
[VARS]
OLD_REL = 4.3
NEW_REL = 4.5

When i tried to get key values it returns only first line

% set fileOrg [ini::open "sample.ini" r]
ini11
% foreach sec [ini::sections $fileOrg] {puts [::ini::get $fileOrg $sec]}
NEW_REL 4.5 OLD_REL 4.3
COMPARE_UNITS {1198100 1198100}
%

I have two question

  1. How to read a multi value form a given key using package inifile
  2. Can i specify a list values in a key?

-Malli


回答1:


The INI file format does not support multiline values. The specification is lines with either a section name in square brackets to start a new section or lines with a keyname followed by an equals sign followed by a value terminated in a line end. Or a comment line.

The tcllib parser splits the file into lines and if the line is not a comment, not a section start and does not contain an equals sign it is discarded.

If you want to include multiple values in an INI file value then you should use some application-specific field separator or multiple keys eg:

[Test.Field]
multi-field = first|second|third
[Test.MultiKey]
multi.1 = first
multi.2 = second
multi.3 = third

The first version could be used as simply as:

set ini [ini::open test.ini r]
set fields [split [ini::value $ini Test.Field multi-field] "|"]


来源:https://stackoverflow.com/questions/29192203/how-to-read-multiline-values-in-inifile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!