如何使用Delphi从INI文件中存储和读取数组数据?

更新于
2026-10-04 22:59:26
3阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计364个文字,预计阅读时间需要2分钟。

在ini文件中写入一个数组到一个Ident中,并将值存储在数组中,可以这样操作:

首先,在ini文件中定义数组:

ini[TestSection]val1=1,2,3,4,5,6,7

然后,在代码中读取并解析这个数组:

python假设使用Python读取ini文件import configparser

创建配置解析器config=configparser.ConfigParser()

读取ini文件config.read('config.ini')

获取指定section和option的值values=config.get('TestSection', 'val1').split(',')

将字符串列表转换为整数列表array=[int(value) for value in values]

输出数组print(array)

这样,你就可以从ini文件中读取数组,并将其存储在Python的列表中。

如何在ini文件中将一个数组写入一个Ident,最近如何从中读取并将值存储在数组中?

这就是我喜欢ini的样子:

[TestSection] val1 = 1,2,3,4,5,6,7

我遇到的问题:

>我不知道我必须使用的功能
>尺寸不是静态的.它可能超过7个值,可能更小.我如何检查长度?

你可以这样做,

uses inifiles procedure ReadINIfile var IniFile : TIniFile; MyList:TStringList; begin MyList := TStringList.Create(); try MyList.Add(IntToStr(1)); MyList.Add(IntToStr(2)); MyList.Add(IntToStr(3)); IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); try //write to the file IniFile.WriteString('TestSection','Val1',MyList.commaText); //read from the file MyList.commaText := IniFile.ReadString('TestSection','Val1',''); //show results showMessage('Found ' + intToStr(MyList.count) + ' items ' + MyList.commaText); finally IniFile.Free; end; finally FreeAndNil(MyList); end; end;

您必须将整数保存并加载为CSV字符串,因为没有内置函数可以将数组直接保存到ini文件中.

本文共计364个文字,预计阅读时间需要2分钟。

在ini文件中写入一个数组到一个Ident中,并将值存储在数组中,可以这样操作:

首先,在ini文件中定义数组:

ini[TestSection]val1=1,2,3,4,5,6,7

然后,在代码中读取并解析这个数组:

python假设使用Python读取ini文件import configparser

创建配置解析器config=configparser.ConfigParser()

读取ini文件config.read('config.ini')

获取指定section和option的值values=config.get('TestSection', 'val1').split(',')

将字符串列表转换为整数列表array=[int(value) for value in values]

输出数组print(array)

这样,你就可以从ini文件中读取数组,并将其存储在Python的列表中。

如何在ini文件中将一个数组写入一个Ident,最近如何从中读取并将值存储在数组中?

这就是我喜欢ini的样子:

[TestSection] val1 = 1,2,3,4,5,6,7

我遇到的问题:

>我不知道我必须使用的功能
>尺寸不是静态的.它可能超过7个值,可能更小.我如何检查长度?

你可以这样做,

uses inifiles procedure ReadINIfile var IniFile : TIniFile; MyList:TStringList; begin MyList := TStringList.Create(); try MyList.Add(IntToStr(1)); MyList.Add(IntToStr(2)); MyList.Add(IntToStr(3)); IniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); try //write to the file IniFile.WriteString('TestSection','Val1',MyList.commaText); //read from the file MyList.commaText := IniFile.ReadString('TestSection','Val1',''); //show results showMessage('Found ' + intToStr(MyList.count) + ' items ' + MyList.commaText); finally IniFile.Free; end; finally FreeAndNil(MyList); end; end;

您必须将整数保存并加载为CSV字符串,因为没有内置函数可以将数组直接保存到ini文件中.