Problem writing a Faster_csv generated record to a file

T

thiel

Hi,

I have a problem writing a record generated with FasterCSV to a file:

This is the code:

op = File.new("C:/Test/testmap.csv", "w")
for i in 0 .. n
for j in 0 .. 12
custrecord[j] = x[i,j]
end
csv_string = FasterCSV.generate_line(custrecord)
csv_string.chop!
csv_string.gsub!(/,/, ';')
op.puts csv_string
end

The result in folder Test is: testmap.csv --> 0Kb!! (Type CSV file)

It seems a very simple program which should work.

Can anyone tell me what's wrong?

Thanks in advance,

Thiel
 
B

Brian Candler

thiel said:
This is the code:

Only part of it - you haven't shown what sort of object custrecord is,
for example.
op = File.new("C:/Test/testmap.csv", "w")
for i in 0 .. n
for j in 0 .. 12
custrecord[j] = x[i,j]
end

STDERR.puts "custrecord = #{custrecord.inspect}"
csv_string = FasterCSV.generate_line(custrecord)
csv_string.chop!
csv_string.gsub!(/,/, ';')
op.puts csv_string
end

The result in folder Test is: testmap.csv --> 0Kb!! (Type CSV file)

That STDERR line will tell you whether custrecord contains what you
expect. If not, work backwards until you find the problem (e.g. look at
what's in x)

There are simpler ways to drive fastercsv than your code though. Try
this:

require 'rubygems'
require 'fastercsv'
FasterCSV.open("/tmp/test.csv", "w", :col_sep=>";") do |csv|
custrecord = ["foo","bar","baz"]
csv << custrecord
end

The documentation for fastercsv is within the code, which may be
installed somewhere like .../gems/fastercsv-1.4.0/lib/faster_csv.rb on
your system.

Or type "gem server --daemon" then point your web browser at
http://localhost:8808/
 
T

thiel

Hi Brian,

Many thanks for your suggestion to use the inspect method. That revealed
a 'beginners' program error. :-D
After having made the correction I applied your solution, and it made
the code simpler. And what's more important: It works!

Here is the code snippet:

FasterCSV.open("c:/Test/test.csv", "w", :col_sep=>";") do |csv|

for i in 0 .. n
for j in 0 .. 12
klantrecord[j] = x[i,j]
end
csv << klantrecord
end
end

Best regards,

Thiel


Brian Candler schreef:
thiel said:
This is the code:

Only part of it - you haven't shown what sort of object custrecord is,
for example.

op = File.new("C:/Test/testmap.csv", "w")
for i in 0 .. n
for j in 0 .. 12
custrecord[j] = x[i,j]
end

STDERR.puts "custrecord = #{custrecord.inspect}"

csv_string = FasterCSV.generate_line(custrecord)
csv_string.chop!
csv_string.gsub!(/,/, ';')
op.puts csv_string
end

The result in folder Test is: testmap.csv --> 0Kb!! (Type CSV file)

That STDERR line will tell you whether custrecord contains what you
expect. If not, work backwards until you find the problem (e.g. look at
what's in x)

There are simpler ways to drive fastercsv than your code though. Try
this:

require 'rubygems'
require 'fastercsv'
FasterCSV.open("/tmp/test.csv", "w", :col_sep=>";") do |csv|
custrecord = ["foo","bar","baz"]
csv << custrecord
end

The documentation for fastercsv is within the code, which may be
installed somewhere like .../gems/fastercsv-1.4.0/lib/faster_csv.rb on
your system.

Or type "gem server --daemon" then point your web browser at
http://localhost:8808/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top