Skip to content

Instantly share code, notes, and snippets.

Created February 14, 2018 00:05
Show Gist options
  • Save anonymous/3dc4a9bd1d53677708b2c11a688f6441 to your computer and use it in GitHub Desktop.
Save anonymous/3dc4a9bd1d53677708b2c11a688f6441 to your computer and use it in GitHub Desktop.
# Expected output
{
name: "Jon",
wallets: {
"EUR": 868.65,
"USD": 463.69
}
}
require 'csv'
require 'json'
class WalletCentral
def self.output
file = File.open("./lib/resources/wallets.csv", "r")
data = file.read
CSV.parse(data).to_json
end
end
Client Currency Amount
jon EUR 868.65
jon USD 463.39
aray EUR 1379.78
aray USD 1907.57
sansa EUR 1065.45
sansa BRL 586.28
jaime EUR 1512.27
jaime USD 1217.4
tyrion USD 1068.12
cersei USD 127.48
cersei EUR 334.13
bronn EUR 108.09
bronn BRL 1341.58
joffrey BRL 1323.87
joffrey EUR 1645.04
eddard EUR 779.36
eddard BRL 106.82
daario EUR 1233.6
littlefinger BRL 1567.04
littlefinger USD 908.9
@havenwood
Copy link

require 'csv'
require 'json'

headers, *rows = CSV.read 'wallet.csv'

result = Hash.new { |h, k| h[k] = {} }
rows.each do |client, currency, amount|
  result[client][currency] = amount
end
result.to_json
#=> "{\"jon\":{\"EUR\":\"868.65\",\"USD\":\"463.39\"},\"aray\":{\"EUR\":\"1379.78\",\"USD\":\"1907.57\"},\"sansa\":{\"EUR\":\"1065.45\",\"BRL\":\"586.28\"},\"jaime\":{\"EUR\":\"1512.27\",\"USD\":\"1217.4\"},\"tyrion\":{\"USD\":\"1068.12\"},\"cersei\":{\"USD\":\"127.48\",\"EUR\":\"334.13\"},\"bronn\":{\"EUR\":\"108.09\",\"BRL\":\"1341.58\"},\"joffrey\":{\"BRL\":\"1323.87\",\"EUR\":\"1645.04\"},\"eddard\":{\"EUR\":\"779.36\",\"BRL\":\"106.82\"},\"daario\":{\"EUR\":\"1233.6\"},\"littlefinger\":{\"BRL\":\"1567.04\",\"USD\":\"908.9\"}}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment