我可以通过number / id而不是名字来获取Powershell对象的noteproperty吗?

时间:2015-08-28 05:07:29

标签: powershell

$s = import-csv AAPL.csv
$s | gm
TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Adj Close   NoteProperty System.String Adj Close=103.739998
Close       NoteProperty System.String Close=103.739998
Date        NoteProperty System.String Date=2015-08-03
High        NoteProperty System.String High=122.57
Low         NoteProperty System.String Low=92.00
Open        NoteProperty System.String Open=121.50
Volume      NoteProperty System.String Volume=82932100

# $ts = $s.'Adj Close'         //OK
# $ts = $s[ 0 .. $s.Count][3]  //Anyway of doing this? suppose 'Adj Close' is on column 4.

我想知道我是否可以通过位置编号/ id获取NoteProperty值。

我在想这是因为我可能会遇到财务数据,这些数据没有“调整关闭”的确切名称,例如“最后关闭”,“关闭”,“调整关闭”等...在同一列上可以使用相同的代码进行分析。

谢谢!

1 个答案:

答案 0 :(得分:1)

要选择具有模糊名称的CSV列,您可以use a regex to select the column names

对于工作样本,我们选择一些列:

# Test data
$data = @'
"Last Close", "Close", "Adjusted Close", "Other Close"
10, 20, 30, 40
50, 60, 70, 80
'@

# Convert here-string into csv so it behaves like import-csv's output
$csvdata = convertfrom-csv $data

# Match only Close header by looking at headers and picking just it
[string[]]$headers = $csvdata | gm -MemberType "noteproperty" | 
                        ?{ $_.Name -match "^Close$"} | 
                        select -expand Name
$csvdata | select $headers
Close
-----
20
60

# Match Last or Adjusted Close with same technique
[string[]]$headers = $csvdata | gm -MemberType "noteproperty" | 
                        ?{ $_.Name -match "^((Last)|(Adjusted)) Close$"} | 
                        select -expand Name
$csvdata | select $headers
Adjusted Close Last Close
-------------- ----------
30             10
70             50