2015-11-18 171 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])]