FasterCSV and " char question

M

Mark Toth

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] ,
:eek:thersku => 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
 
J

James Gray

- The problem is that fasterCSV read the file before it=B4s been =20
converted
with the " char.

If you're using a CSV parser and you are having to manually translate =20=

the quote, the data probably isn't really CSV data. If you just have =20=

a bunch of fields separated by comma (and they may have quotes in =20
them), you don't need a CSV parser at all:

fields =3D line.split(",")

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

line.delete!('"')
- The second problem that I have with Rails is that =E5=E4=F6 letters = doesn=20
=B4t
been imported correctly even when I use the $KCODE =3D "utf8" code.

Check to be sure that the file data actually is UTF-8 encoded and that =20=

your database tables are setup for that encoding.

Hope that helps.

James Edward Gray II=
 
M

Mark Toth

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] ,
:eek:thersku => row[8] ,
:producturl => row[9] ,
:stockstatus => row[10] ,
:shipping => row[11])
a_product.save
end
end

Any idea?
 
M

Mark Toth

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] ,
:eek:thersku => row[8] ,
:producturl => row[9] ,
:stockstatus => row[10] ,
:shipping => row[11])
a_product.save
end
end
@time_end = Time.now
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top