# FasterCSV and " char question

**URL:** <https://www.ruby-forum.com/t/fastercsv-and-char-question/124419>\
**Category:** Ruby\
**Created:** [December 28, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/fastercsv-and-char-question/124419 "2007-12-28T16:12:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [December 28, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/fastercsv-and-char-question/124419/1 "2007-12-28T16:12:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [December 28, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/fastercsv-and-char-question/124419/2 "2007-12-28T16:39:26Z")

</div>

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

> - 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 G. II

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [December 28, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/fastercsv-and-char-question/124419/3 "2007-12-28T17:35:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [December 28, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/fastercsv-and-char-question/124419/4 "2007-12-28T18:23:22Z")

</div>

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
```
