FasterCSV and " char question

Hello,

I have a text that containing " char and want to delete it from the
text before I read it to the database with fasterCSV.

This is "the Text".
===>
This is the Text.

What is the command to simple replace the " char with nothing?

This is the code so you can see what I am trying to do:

    file = 'c:\filename.txt'
    $KCODE = "utf8"
    FasterCSV.foreach(file.tr('"', ''), :headers =>true, :col_sep
=>"\t", :row_sep =>"\n") do |row|
    @PriceListFileRow = row
    next if row.empty?
    a_product = Product.new(
        :sku => row[0] ,
        :name => row[1] ,
        :price => row[2] ,
        :salestart => row[3] ,
        :saleend => row[4] ,
        :category => row[5] ,
        :manufacturer => row[6] ,
        :manufacturersku => row[7] ,
        :othersku => row[8] ,
        :producturl => row[9] ,
        :stockstatus => row[10] ,
        :shipping => row[11])
    a_product.save

- The problem is that fasterCSV read the file before it´s been converted
with the " char.
- The second problem that I have with Rails is that åäö letters doesn´t
been imported correctly even when I use the $KCODE = "utf8" code.

Thank you again and waiting for your answer!

Best regards,

Mark

···

--
Posted via http://www.ruby-forum.com/.

- The problem is that fasterCSV read the file before it´s been converted
with the " char.

If you're using a CSV parser and you are having to manually translate the quote, the data probably isn't really CSV data. If you just have a bunch of fields separated by comma (and they may have quotes in them), you don't need a CSV parser at all:

   fields = line.split(",")

To answer your question though, you can remove quotes with:

   line.delete!('"')

- The second problem that I have with Rails is that åäö letters doesn´t
been imported correctly even when I use the $KCODE = "utf8" code.

Check to be sure that the file data actually is UTF-8 encoded and that your database tables are setup for that encoding.

Hope that helps.

James Edward Gray II

···

On Dec 28, 2007, at 9:12 AM, Mark Toth wrote:

Thanks for your answer. It should be very easy to import this file. The
columns are separated with TAB and the lines with \n .
I have changed my code to the following, but not working... (I´m a
newbie):

    file.split("\n").each do |line|
      line.split("\t").each do |row|
      next if row.empty?
      a_product = Product.new(
        :sku => row[0] ,
        :name => row[1] ,
        :price => row[2] ,
        :salestart => row[3] ,
        :saleend => row[4] ,
        :category => row[5] ,
        :manufacturer => row[6] ,
        :manufacturersku => row[7] ,
        :othersku => row[8] ,
        :producturl => row[9] ,
        :stockstatus => row[10] ,
        :shipping => row[11])
    a_product.save
    end
    end

Any idea?

···

--
Posted via http://www.ruby-forum.com/.

I have now successefully imported the file with the following code:
Thanks for your help!

    file = "filename.txt"
    @time_start = Time.now
    File.open(file) do |sFile|
    @PriceListFileEmpty = file.empty?()
    @PriceListFileLength = file.length()
    @PriceListFileSize = file.size()
    $KCODE = "utf8"

      while line = sFile.gets
      row = line.split("\t")
      next if row.empty?
    a_product = Product.new(
        :sku => row[0] ,
        :name => row[1] ,
        :price => row[2] ,
        :category => row[5] ,
        :manufacturer => row[6] ,
        :manufacturersku => row[7] ,
        :othersku => row[8] ,
        :producturl => row[9] ,
        :stockstatus => row[10] ,
        :shipping => row[11])
    a_product.save
    end
    end
    @time_end = Time.now

···

--
Posted via http://www.ruby-forum.com/.