[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

FasterCSV and " char question

Mark Toth

12/28/2007 3:12:00 PM

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

3 Answers

James Gray

12/28/2007 3:38:00 PM

0

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

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

Mark Toth

12/28/2007 4:35:00 PM

0

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

Mark Toth

12/28/2007 5:23:00 PM

0

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