ruby - How do I read a CSV file? -


i have problems reading csv file 2 columns separated "\tab".

my code is:

require 'csv' require 'rubygems'  # globals infinity = 1.0/0  if __file__ == $0   # locals   data = []   fn = ''    # argument check   if argv.length == 1     fn = argv[0]   else     puts 'usage: kmeans.rb input-file'     exit   end    # data   csv.foreach(fn) |row|     x = row[0].to_f     y = row[1].to_f      p = point.new(x,y)     data.push p   end    # determine number of clusters find   puts 'number of clusters find:'   k = stdin.gets.chomp!.to_i    # run algorithm on data   clusters = kmeans(data, k)    # graph output running gnuplot pipe   gnuplot.open |gp|     # start new plot     gnuplot::plot.new(gp) |plot|       plot.title fn        # plot each cluster's points       clusters.each |cluster|         # collect x , y coords cluster         x = cluster.points.collect {|p| p.x }         y = cluster.points.collect {|p| p.y }          # plot w/o title (clutters things up)         plot.data << gnuplot::dataset.new([x,y]) |ds|           ds.notitle         end       end     end   end end 

the file is:

48.2641334571   86.4516903905 0.1140042627    35.8368597414 97.4319168245   92.8009240744 24.4614031388   18.3292584382 36.2367675367   32.8294024271 75.5836860736   68.30729977 38.6577034445   25.7701728584 28.2607136287   64.4493377817 61.5358486771   61.2195232194 

i'm getting error:

   test.csv:1: syntax error, unexpected ',', expecting $end 48.2641334571,86.4516903905               ^ 

you missing end @ bottom. first if not closed.

csv "comma-separated values". yours using tabs. not big problem, because csv class can handle it, need specify separator tab:

csv.foreach(fn, { :col_sep => "\t" }) 

be sure double-check file using tabs, not spaces not same.

i'm still confused error message, received?


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -