2015-08-28 45 views
0
$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=0 

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

我想知道是否可以通過位置編號/ id獲取NoteProperty值。我可以通過編號/編號而不是名稱獲取Powershell對象的noteproperty嗎?

我之所以這樣想,是因爲我可能遇到的財務數據沒有「調整關閉」的確切名稱,例如「最後關閉」,「關閉」,「調整關閉」等......在同一列並且可以使用相同的代碼進行分析。

謝謝!

+0

請問'$ S |選擇-property *關閉*'爲此工作,還是更多樣化? – Eris

+0

不完全,它將包含'調整關閉'和關閉列。但是,無論如何,你激勵我尋求答案!只要'$ s |選擇「特性」「調節關閉」,「價格」,「等等......」 – user3168078

回答

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