从 CSV 创建变量 [英] Create variable from CSV

查看:55
本文介绍了从 CSV 创建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 CSV 中的特定列创建变量.

I want to make variables from a particular column in a CSV.

CSV 将具有以下标题:

CSV will have the following headers:

FolderName,FolderManager,RoleGroup,ManagerEmail

FolderName 下将是具有各自文件夹名称的行列表,例如:Accounts、HR、Projects 等...(这些名称中的每一个都是一个单独的FolderName 列中的行)

Under FolderName will be a list of rows with respective folder names such as: Accounts,HR,Projects, etc... (each of these names is a separate row in the FolderName column)

所以我想创建一个变量列表,以便在稍后阶段调用.它们将类似于以下内容:

So I would like to create a list of variables to call on in a later stage. They would be something like the following:

$Accounts,
$HR,
$Projects,

我根据此处和谷歌的搜索做了一些不同的脚本,但无法产生想要的结果.我希望有人能引导我找到正确的方向来创建这个脚本.

I have done a few different scripts based on searching here and google, but unable to produce the desired results. I am hoping someone can lead me in the right direction here to create this script.

推荐答案

这个问题的版本(动态变量"或变量变量"或在运行时创建变量")出现了很多,几乎在所有情况下它们不是正确的答案.

Versions of this question ("dynamic variables" or "variable variables" or "create variables at runtime") come up a lot, and in almost all cases they are not the right answer.

不知道解决问题的更好方法的人经常会问这个问题,但有一个更好的方法:集合.数组、列表、哈希表等

This is often asked by people who don't know a better way to approach their problem, but there is a better way: collections. Arrays, lists, hashtables, etc.

问题来了:您想读取用户名并将其打印出来.你不能写Hello Alice,因为你不知道他们的名字是什么来放入你的代码中.这就是变量存在的原因:

Here's the problem: You want to read a username and print it out. You can't write Hello Alice because you don't know what their name is to put in your code. That's why variables exist:

$name = Read-Host "Enter your name"
Write-Host "Hello $name"

太好了,您可以在源代码中编写 $name,这是永远不会改变的.它引用了他们的名字,这确实发生了变化.不过没关系.

Great, you can write $name in your source code, something which never changes. And it references their name, which does change. But that's OK.

但是你被困住了——如果你只有$name,你怎么会有两个人的名字?你怎么能做出很多变量,比如$name2, $name3?你如何制作 $Alice, $Bob?

But you're stuck - how can you have two people's names, if all you have is $name? How can you make many variables like $name2, $name3? How can you make $Alice, $Bob?

可以...

New-Variable -Name (Read-Host "Enter your name") -Value (Read-Host "Enter your name again")

Write-Host "Hello 

等待

你在那里写什么来写他们的名字?您将直接回到变量旨在解决的原始问题.你有一个固定的东西要放在你的源代码中,这让你可以处理一个不断变化的值.

What do you put there to write their name? You're straight back to the original problem that variables were meant to solve. You had a fixed thing to put in your source code, which allowed you to work with a changing value.

现在你有一个不同的东西,你不能在你的源代码中使用它,因为你又不知道它是什么了.

and now you have a varying thing that you can't use in your source code because you don't know what it is again.

毫无价值.

解决方法是一个具有固定名称的变量可以引用集合中的多个值.

And the fix is that one variable with a fixed name can reference multiple values in a collection.

数组 (获取帮助about_Arrays):

$names = @()

do {
    $name = Read-Host "Enter your name"
    if ($name -ne '')
    {
        $names += $name
    }
} while ($name -ne '')

# $names is now a list, as many items long as it needs to be. And you still 
# work with it by one name.

foreach ($name in $names) 
{ 
    Write-Host "Hello $name" 
}

# or

$names.Count

or

$names | foreach { $_ }

还有更多的收藏,比如

哈希表 (获取帮助about_Hash_Tables):键 -> 值对.让我们将文件夹中的每个文件与其大小配对:

Hashtables (Get-Help about_Hash_Tables): key -> value pairs. Let's pair each file in a folder with its size:

$FileSizes = @{}   # empty hashtable. (aka Dictionary)

Get-ChildItem *.txt | ForEach {

    $FileSizes[$_.BaseName] = $_.Length

}

# It doesn't matter how many files there are, the code is just one block 

# $FileSizes now looks like
@{
    'readme' = 1024;
    'test' = 20;
    'WarAndPeace' = 1048576;
}

# You can list them with

$FileSizes.Keys

and

foreach ($file in $FileSizes.Keys)
{
    $size = $FileSizes[$file]

    Write-Host "$file has size $size"
}

不需要每个文件或每个文件名的动态变量.一个固定名称,一个适用于任意数量值的变量.您需要做的就是添加多少有多少"和处理多少就有多少",而无需明确关心有多少.

No need for a dynamic variable for each file, or each filename. One fixed name, a variable which works for any number of values. All you need to do is "add however many there are" and "process however many there are" without explicitly caring how many there are.

而且你永远不需要问现在我已经为我所有的东西创建了变量名......我如何找到它们?"因为你在你放入的集合中找到这些值.通过列出所有这些值,从头开始搜索直到找到一个,通过使用 -match-in 过滤它们-contains.

And you never need to ask "now I've created variable names for all my things ... how do I find them?" because you find these values in the collection you put them in. By listing all of them, by searching from the start until you find one, by filtering them, by using -match and -in and -contains.

是的,New-Variable 和 Get-Variable 有它们的用途,如果您了解集合并想使用它们,也许您确实对它们有用.

And yes, New-Variable and Get-Variable have their uses, and if you know about collections and want to use them, maybe you do have a use for them.

但我认为 StackOverflow 上的很多人问这个问题仅仅是因为他们还不了解集合.

But I submit that a lot of people on StackOverflow ask this question solely because they don't yet know about collections.

Powershell 中的动态变量

在 Powershell 中增加动态变量

powershell 中的动态变量和值分配

在 PowerShell 中动态使用变量

如何基于动态变量在 Powershell 中创建和填充数组?

还有更多,在 Python 中:

And many more, in Python too:

https://stackoverflow.com/a/5036775/478656

如何通过while动态创建变量循环?

这篇关于从 CSV 创建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆