gpt4 book ai didi

powershell - 将 ConvertFrom-String 与所有数据集中不存在的值一起使用

转载 作者:行者123 更新时间:2023-12-03 14:34:27 25 4
gpt4 key购买 nike

我有一个文本文件,其中包含结构化数据(类似于 Json),我想用 ConvertFrom-String 解析它以提取部分数据。我无法更改文件中数据的结构方式,因此我必须按原样处理它们。

这是数据的示例。完整的文件有更多这样的块,每个块都有比我在这里展示的更多的“属性”(保持小而可读:-))。请注意,第三个块为 icon其他元素没有的属性。

$Data = @'
c_first_item = {
time = 270
category = cat_red
min_value = {
int = 10
}
max_value = {
int = 20
}
}

c_second_item = {
time = 270
category = cat_blue
min_value = {
float = 10
}
max_value = {
float = 20
}
}

c_third_item = {
time = 270
icon = c_third_icon
category = cat_red
min_value = {
int = 10
}
max_value = {
int = 20
}
}
'@

这是我的 ConvertFrom-String 使用的模板。
    $Template = @'
{Name*:c_first_item} = \{
time = 270

category = {Category:cat_red}


random_property = \{
random_property_value = 10
\}

random_property = \{
random_property_value = 20
\}
\}

{Name*:c_second_item} = \{
time = 100
icon = {Picture?:c_third_icon}

category = {Category:cat_blue}

random_property = \{
random_property_value = 10
\}

random_property = \{
random_property_value = 20
\}
\}
'@

我用 ConvertFrom-String 运行此代码解析我想要的数据:
$Result = $Data | ConvertFrom-String -TemplateContent $Template

预期的结果应该是这样的:
Name          Category  Picture
---- -------- -------
c_first_item cat_red
c_second_item cat_blue
c_third_item cat_red c_third_icon

但我得到了这个:
Name          Category
---- --------
c_first_item cat_red
c_second_item cat_blue
c_third_item cat_red

如您所见,没有 Picture我的输出对象中的属性为 ConvertFrom-String无法根据我的要求解析数据。我想我给出的例子不足以教算法如何使用react。

有没有办法教我 ConvertFrom-String得到一个空 Picture块的属性,其中 icon不存在?

更新:1 月 25 日

根据@iRon 的要求,这是一个添加 Length 的示例结果的属性。

首先,我正在解析的文件 (Test.txt) :
d_mem_towers_sulfuric_forest = {
is_for_colonizeable = yes
category = deposit_cat_rare

icon = d_radioactive_wasteland

resources = {
category = planet_deposits
produces = {
society_research = 4
energy = 4
}
}

planet_modifier = {
planet_jobs_energy_produces_mult = 0.10
planet_jobs_society_research_produces_mult = 0.10
planet_max_districts_add = -1
pop_environment_tolerance = -0.15
}

drop_weight = {
weight = 0
}
}

然后,模板文件(Template.txt):
{Object*:{Name:d_mem_sadrell_capital} = \{
is_for_colonizeable = yes
category = {Category:deposit_cat_rare}

icon = {Picture:d_building}

planet_modifier = \{
planet_housing_add = 3
pop_environment_tolerance = 0.1
planet_jobs_specialist_produces_mult = 0.15
\}

triggered_planet_modifier = \{
potential = \{
exists = owner
owner = \{ is_regular_empire = yes \}
\}
modifier = \{
job_researcher_add = 2
job_clerk_add = 2
\}
\}

triggered_planet_modifier = \{
potential = \{
exists = owner
owner = \{ is_hive_empire = yes \}
\}
modifier = \{
job_brain_drone_add = 2
job_maintenance_drone_add = 2
\}
\}

triggered_planet_modifier = \{
potential = \{
exists = owner
owner = \{ is_machine_empire = yes \}
\}
modifier = \{
job_calculator_add = 2
job_maintenance_drone_add = 2
\}
\}

drop_weight = \{
weight = 0
\}
\}}

{Object*:{Name:d_mem_sadrell_industrial_complex} = \{
is_for_colonizeable = yes
category = {Category:deposit_cat_rare}

icon = {Picture:d_crater}

planet_modifier = \{
pop_environment_tolerance = -0.1
planet_jobs_minerals_produces_mult = 0.25
district_mining_max = 4
\}

drop_weight = \{
weight = 0
\}
\}}

{Object*:{Name:_mem_strange_mountain} = \{
time = 365
is_for_colonizeable = yes
category = {Category:deposit_cat_blockers}

icon = {Picture:d_mem_strange_mountain}

resources = \{
category = deposit_blockers
cost = \{
energy = 1000
minerals = 1000
\}
\}

drop_weight = \{
weight = 0
\}
\}}

最后,这是我用来提取数据的一段代码:
$testText = Get-Content  -Path "Path\to\text.txt" #MEM
$template = 'Path\to\template.txt'

$result = $testText |
ConvertFrom-String -TemplateFile $template |
Select-Object -ExpandProperty Object |
Union-Object

$result

预期的结果应该是这样的:
Name                         Category         Picture               
---- -------- -------
d_mem_towers_sulfuric_forest deposit_cat_rare d_radioactive_wasteland

但我得到了这个:
Name                         Category         Picture                 Length
---- -------- ------- ------
d_mem_towers_sulfuric_forest deposit_cat_rare d_radioactive_wasteland
0

我的对象的第二行包含 Length我不解析的属性,所以我猜它以某种方式来自 Union-Object .

任何的想法?

最佳答案

尽管您的所有属性(包括 Picture )都在 $Result 中对象,它们不会被输出(到显示器),因为并非所有(尤其是第一个)对象都包含 Picture属性(参见相关问题:Not all properties displayed):

PS C:\> $Result

Name Category
---- --------
c_first_item cat_red
c_second_item cat_blue
c_third_item cat_red

Union-Object cmdlet将所有缺失的属性(带有 $Null 值)添加到相关对象:
PS C:\> $Result = $Result | Union-Object
PS C:\> $Result

Name Category Picture
---- -------- -------
c_first_item cat_red
c_second_item cat_blue
c_third_item cat_red
cat_blue

关于powershell - 将 ConvertFrom-String 与所有数据集中不存在的值一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54136700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com