导入-CSV和创建散列表失败 [英] Import-Csv and creating hashtable failing
问题描述
我的CSV文件包含员工数据,每个人都有两个代码,一个用于登录,另一个用于其位置ID。要找到他们的经理,我需要查找Reports到PositionID表作为交叉引用。我遇到的问题是当我使用 Import-Csv
,文件中的第一个用户终止,主登录ID从文件中删除。我想这就是为什么下面的哈希表失败,索引操作失败;数组索引评估为空?
My CSV file has employee data and each person gets 2 codes, one for login and one for their positionID. To find their manager, I need to lookup the Reports to PositionID table as a cross reference. The problem I'm having is when I use Import-Csv
, the first user in the file is terminated and the main login ID is removed from the file. I think that's why the following hash table fails with "Index operation failed; the array index evaluated to null"?
我试过
$csvData = Get-Content -Path $Maincsv | Select-Object -Skip 2 |
Out-String | ConvertFrom-Csv
但这也失败了。
$csvData = Import-Csv "C:\My.csv"
$PIDTable = @{}
$csvData | ForEach-Object {
$PIDTable[$_."Position ID"] = $_
}
b $ b
数据:
Data:
PersonID Position ID Legal First Name Reports To Position ID
YQM000051 DIANE YQM000076
S9999991 YQM000052 CHARISSE YQM000076
S9999992 YQM000052 CHARISSE YQM000076
s9999993 YQM000052 CHARISSE YQM000076
s9999994 YQM000076 Bob YQM000071
所以,试图穿越参考PersonID从报告到位置ID,以便可以找到人员的经理职位ID。对不起,凌乱的格式。但是可以在开始的时候空白的PersonID,弄乱阵列吗?
So, trying to cross reference the PersonID from "Reports To Position ID" so that a PersonID could be found for someone's manager Position ID. Sorry for messy format. But can the blank PersonID at the start, mess the array up?
推荐答案
您的数据似乎是以制表符分隔的,而不是逗号分隔。 Import-Csv
预期以逗号分隔数据,除非通过参数 -Delimiter
指定不同的分隔符。因为你没有这样做,cmdlet会将文件读入数据结构,如下所示:
Your data appears to be tab-separated, not comma-separated. Import-Csv
expects data to be comma-separated unless you specify a different delimiter via the parameter -Delimiter
. Since you didn't do that the cmdlet reads the file into data structures like this:
{ "PersonID Position ID Legal First Name Reports To Position ID": " YQM000051 DIANE YQM000076" }
{ "PersonID Position ID Legal First Name Reports To Position ID": "S9999991 YQM000052 CHARISSE YQM000076" }
...
而不是预期的数据结构:
rather than the intended data structures:
{
"PersonID": ""
"Position ID": "YQM000051"
"Legal First Name": "DIANE"
"Reports To Position ID": "YQM000076"
}
{
"PersonID": "S9999991"
"Position ID": "YQM000052"
"Legal First Name": "CHARISSE"
"Reports To Position ID": "YQM000076"
}
...
因为您的对象没有属性 Postition ID
。尝试从这些缺失的属性创建哈希表条目会导致您观察到的错误。
Because of that your objects don't have a property Postition ID
. Trying to create hashtable entries from these missing properties causes the errors you observed.
更改
$csvData = Import-Csv "C:\My.csv"
$csvData = Import-Csv "C:\My.csv" -Delimiter "`t"
,您的代码应该按预期运行。
and your code should behave as you expect.
这篇关于导入-CSV和创建散列表失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!