peterc (owner)

Revisions

gist: 110186 Download_button fork
public
Public Clone URL: git://gist.github.com/110186.git
Embed All Files: show embed
csvtest.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require 'rubygems'
 
if RUBY_VERSION >= "1.9.0"
  require 'csv'
else
  require 'fastercsv'
  CSV = FasterCSV
end
 
# Write out dummy data file
File.open("data.csv", "w") do |f|
  f.puts %{Name,Age,Gender,Location
Clive,53,male,UK
Ann,55,female,France
Eugene,29,male,California}
end
 
# Load CSV
csv = CSV.read('data.csv', :headers => true)
 
# Test 1
csv.each do |row|
  puts row['Name']
end
 
# Test 2
csv.each do |row|
  row['Location'] = "Nowhere"
end
puts csv.to_csv