2015-11-18 208 views
0

我已經下載使用CSV文件HTTParty並在本地保存的文件,所以我可以在以後的日子檢查它,但它看來,如果數據格式不正確格式化CSV文件格式正確

[["Team Name", "User Name", "Dataset Name", "No of Searches", "Credits Remaining"], ["", "", 
"DRI", "129", "99085"], ["", "", "Property Register Search (G)", "124", "99414"], ["", "", 
"Landline Verification", "1", "99783"], ["", "", 
"Equifax (G)", "372", "97798"], ["", "", "Director Register", "135", "98499"], ["", "", 
"Mobile Verification", "2", "99845"], ["", "", 
"BT OSIS", "428", "91588"], ["", "", 
"Experian (G)", "97", "99913"], ["", "", "Standard (G)", 
"873", "82151"], ["", "", "CCJ", "120", "98367"]] 

這樣我就可以使用紅寶石提供的CSV類,我需要的數據是以下列格式嗎?

Team Name, User Name, Dataset Name, No of Searches, Credits Remaining 
"", "", DRI, 129, 99085 
"", "", Property Register Search (G), 124, 99414] 
"", "", Landline Verification, 1, 99783 
"", "", Equifax (G), 372, 97798 
"", "", Director Register, 135, 98499 
"", "", Mobile Verification, 2, 99845 
"", "", BT OSIS, 428, 91588] 
"", "", Experian (G), 97, 99913 
"", "", Standard (G), 873, 82151 
"", "", CCJ, 120, 98367 

什麼,我希望做到的,是得到一個地步,我可以湊了這一點,所以我可以爲Dataset NameStandard

希望訪問Credits Remaining有道理

感謝

UPDATE

感謝@mudasobwa爲您的答案,我現在有我的csv文件內容在一個哈希值的N排列的(我認爲:))

{"TeamName"=>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], 
"UserName"=>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], 
"DatasetName"=> ["DRI", "PropertyRegisterSearch(G)", "LandlineVerification","Equifax(G)", "DirectorRegister", "MobileVerification", "BTOSIS", "Experian(G)", "Standard(G)","CCJ"], 
"NoofSearches"=>["129", "124", "1", "372", "135", "2", "428", "97", "873", "120"], 
"CreditsRemaining"=>["99085", "99414", "99783", "97798", "98499", "99845", "91588", "99913", "82151", "98367"] 
} 

我該如何獲得NoofSearchesDatasetNameDRI對應的,所以我希望得到129返回

+0

我不確定ruby需要什麼,但是您可以輕鬆將第一個轉換爲第二個。編寫一個快速程序刪除所有'['括號,並用換行符替換']'。 –

+0

這是第一個代碼示例,您下載的CSV或使用Ruby生成的東西? –

+0

因此,數據似乎與我一致,重新格式化您的數組顯示此: –

回答

1

這個例子應該把您的CSV到一個數組可以通過前colum名稱訪問數據。

data = [] 

CSV.foreach('test.csv', headers: true) { |row| data << row.to_hash } 

data.inspect 

=> [{:col1=>'value1', :col2=>'value2', :col3=> 'value3'}, 
    {:col1=>'value4', :col2=>"value5", :col3=>'value6'}] 

data.csv內容是這樣的:

col1,col2,col3 
value1,value2,value3 
value4,value5,value6 
+0

'NoMethodError:undefined method'to_hash'for#' – mudasobwa

+0

您的csv的內容看起來如何?與試了一下: COL1,COL2,COL3 值1,值2,值3 值4,值5,value6 –

+0

我會得到相同的未定義的方法 – Richlewis

1
▶ csv = [["Team Name", "User Name", "Dataset Name", "No of Searches", "Credits Remaining"], ["", "", 
▷ "DRI", "129", "99085"], ["", "", "Property Register Search (G)", "124", "99414"], ["", "", 
▷ "Landline Verification", "1", "99783"], ["", "", 
▷ "Equifax (G)", "372", "97798"], ["", "", "Director Register", "135", "98499"], ["", "", 
▷ "Mobile Verification", "2", "99845"], ["", "", 
▷ "BT OSIS", "428", "91588"], ["", "", 
▷ "Experian (G)", "97", "99913"], ["", "", "Standard (G)", 
▷ "873", "82151"], ["", "", "CCJ", "120", "98367"]] 

那麼下面會給你想要的東西:

▶ csv.transpose.map { |e| [e.shift, e] }.to_h 

或:

▶ csv.transpose.group_by(&:shift).map { |k, v| [k, v.first] }.to_h 

要訪問NoofSearchesDatasetNameDRI對應於:

▶ hash = csv.transpose.map { |e| [e.shift, e] }.to_h 
# ⇓ lookup array of noofs 
#      ⇓ by index of 'DRI' in 'Dataset Name' 
▶ hash['No of Searches'][hash['Dataset Name'].index('DRI')] 
+0

好吧我已經到了一個哈希創建的狀態(需要了解轉置正在做什麼,將查看),但我如何訪問說'分配給'DatasetName'標準'的搜索數量' – Richlewis

+0

我已經更新了我的問題,請問能否看一下,謝謝您的幫助,謝謝 – Richlewis

+0

請參閱更新。 – mudasobwa

0

使用陣列#拉鍊另一種解決方案。

顯然,您下載的文件不是CSV格式。但是,看起來像文件中的字符串可以直接評估到Ruby數組中,即使它很亂。

#!/usr/bin/env ruby 

file = File.open("test.data", "r") 
#NOTE: eval is evil! 
csv_arrs = eval(file.read.gsub("\n", "")) 
file.close 

headers = csv_arrs.shift 
query = { 
    :select => "No of Searches", 
    :key => "Dataset Name", 
    :value => "DRI" 
} 

r = csv_arrs.find {|a| Hash[ headers.zip(a) ][ query[:key] ] == query[:value]} 
puts r[headers.index(query[:select])]