Below is code using Magento's native methods to parse your CSV. I do notice in your function you are indicating that a comma
is your field delimiter but in the file structure sample you show a |
symbol as your delimiter.
If the pipe symbol is actually your delimiter, then below you would use $processor->streamReadCsv('|');
as the default delimiter for that function is a comma. Additionally, if your fields are not enclosed in double quotes, you should enter null
as the second argument because "
is the default field enclosure. So it would be like this $processor->streamReadCsv('|', null);
Finally, if your fields are not enclosed in double quotes, it is strongly recommended that you do enclose them in double quotes. Ideally, your file structure should look more like this:
"Sno.","Description","Sku"
-
$file = 'path/to/your/file.csv';
$processor = new Varien_Io_File();
$processor->streamOpen($file, 'r');
while($row = $processor->streamReadCsv()) {
// do whatever you need to do here with the csv row data
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $row[2]);
$product->setData('description',$row[1]);
$product->save();
}
$processor->streamClose();
Also, notice that I used $product->setData('description',$row[1]);
instead of $product->setDescription($row[1]);
. Thats because when you are parsing large amounts of data, you would just be giving Magento more work to do because it has to do some converting of the non-existent method setDescription('content')
into setData('description','content')
Update to answer
After trying with your file specifically, it appears that you need to UTF-8 encode your data. Additionally, ALL fields need to be surrounded by double quotes, especially since your data is not contained on one line but instead you have one product taking up several lines in the text file.
If you are using microsoft excel that wont work because excel strips quotes. You can use a free product like OpenOffice.org Calc to ensure that the "
double quotes don't get stripped out of your csv. With that being said, here is the code that worked for me with your file specifically.
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$file = 'path/to/your/file.csv';
$processor = new Varien_Io_File();
$processor->streamOpen($file, 'r');
while($row = $processor->streamReadCsv()) {
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $row[2]);
$product->setData('description',utf8_encode($row[1]));
$product->save();
}
$processor->streamClose();