<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: csv code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 23:08:46 GMT</pubDate>
    <description>DZone Snippets: csv code</description>
    <item>
      <title>QIF to CSV conversion script in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5409</link>
      <description>// Converts QIF files to CSV files&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'fileutils'&lt;br /&gt;&lt;br /&gt;if ARGV.size &lt; 1&lt;br /&gt;        puts "Usage: #{$0} file.qif"&lt;br /&gt;        exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;input = File.new(ARGV[0])&lt;br /&gt;output = [File.basename(ARGV[0]).split('.')[0..-2], 'csv'].join('.')&lt;br /&gt;output = File.new(output, 'w+')&lt;br /&gt;output.write("date,amount,description,transaction id, address\n")&lt;br /&gt;&lt;br /&gt;entries = input.read.split("^\n")&lt;br /&gt;entries.compact&lt;br /&gt;for entry in entries&lt;br /&gt;        e = entry.match(/D(.*)\nT-?(.*)\nP(.*)\nN(.*)\nA(.*)\n/).to_a[1..-1]&lt;br /&gt;        e[1] = e[1].to_f rescue nil&lt;br /&gt;        e[-1] = "\"#{e[-1]}\""&lt;br /&gt;        output.write("#{e.join(',')}\n")&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 20 Apr 2008 21:34:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5409</guid>
      <author>shawnpyle (Shawn Pyle)</author>
    </item>
    <item>
      <title>How to produce daily database table dumps in CSV format</title>
      <link>http://snippets.dzone.com/posts/show/4790</link>
      <description>The following code demonstrates how to produce CSV files with dynamic&lt;br /&gt;file name pattern based on a current day. The produced files have the following naming format:&lt;br /&gt;TABLE_NAME_MM_DD_YYYY.csv&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd"&gt;&lt;br /&gt;&lt;etl&gt;&lt;br /&gt;    &lt;properties&gt; &lt;!-- Configure table name --&gt;&lt;br /&gt;        table_name=test&lt;br /&gt;    &lt;/properties&gt;&lt;br /&gt;    &lt;connection id="in" driver="auto" url="jdbc:oracle:thin:@localhost:1521:ORCL" &lt;br /&gt;      classpath="ojdbc14.jar" user="scott" password="tiger"/&gt;&lt;br /&gt;    &lt;connection id="out" driver="csv" url="${table_name}_${etl.date.now('MM_dd_yyyy')}.csv" /&gt;&lt;br /&gt;    &lt;query connection-id="in"&gt; &lt;!-- Query table rows --&gt;&lt;br /&gt;        SELECT * FROM ${table_name}&lt;br /&gt;        &lt;script connection-id="out"&gt; &lt;!-- Export each row into a CSV --&gt;&lt;br /&gt;            $ID, $Name, $Surname &lt;!-- Use column names from selected table --&gt;&lt;br /&gt;        &lt;/script&gt;&lt;br /&gt;    &lt;/query&gt;&lt;br /&gt;&lt;/etl&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Use &lt;a href="http://scriptella.javaforge.com"&gt;Scriptella ETL&lt;/a&gt; to run the example. &lt;br /&gt;&lt;br /&gt;See &lt;a href="http://snippets.dzone.com/posts/show/4862"&gt;How to execute an ETL file&lt;/a&gt; from command line, Ant or directly from Java .</description>
      <pubDate>Fri, 16 Nov 2007 21:08:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4790</guid>
      <author>ejboy (Fyodor Kupolov)</author>
    </item>
    <item>
      <title>CSV parsing regex</title>
      <link>http://snippets.dzone.com/posts/show/4430</link>
      <description>The regular expression is taken from Raimond Brookman, &lt;a href="http://blogs.infosupport.com/raimondb/archive/2005/04/27/199.aspx"&gt;Regex fun with CSV&lt;/a&gt;.&lt;br /&gt;For a good general CSV overview see &lt;a href="http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm"&gt;The Comma Separated Value (CSV) File Format&lt;/a&gt;.&lt;br /&gt;A complete Ruby CSV parsing library is &lt;a href="http://fastercsv.rubyforge.org"&gt;FasterCSV&lt;/a&gt; (sudo gem install fastercsv).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;csv_data = &lt;&lt;-EOS&lt;br /&gt;&lt;br /&gt;fname,lname,age,salary&lt;br /&gt;nancy,davolio,33,$30000&lt;br /&gt;erin,borakova,28,$25250&lt;br /&gt;tony,raphael,35,$28700&lt;br /&gt;&lt;br /&gt;"Date","Pupil","Grade"&lt;br /&gt;"25 May","Bloggs, Fred","C"&lt;br /&gt;"25 May","Doe, Jane","B"&lt;br /&gt;"15 July","Bloggs, Fred","D"&lt;br /&gt;&lt;br /&gt;123456789,"Carr, Lisa",100000.00&lt;br /&gt;444556666,"Barr, Clark",87000.00&lt;br /&gt;777227878,"Parr, Jack",123000.00&lt;br /&gt;998877665,"Charr, Lee",123000.00&lt;br /&gt;&lt;br /&gt;Conference room 1, "John,  &lt;br /&gt;Please bring the M. Mathers file for review  &lt;br /&gt;-J.L.&lt;br /&gt;"&lt;br /&gt;10/18/2002,...&lt;br /&gt;&lt;br /&gt;John,Doe,120 jefferson st.,Riverside, NJ, 08075&lt;br /&gt;Jack,McGinnis,220 hobo Av.,Phila, PA,09119&lt;br /&gt;"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075&lt;br /&gt;Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234&lt;br /&gt;,Blankman,,SomeTown, SD, 00298&lt;br /&gt;"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123&lt;br /&gt;&lt;br /&gt;XXXX,D,3-May-02,83.01,83.58,71.13,78.04,9645300&lt;br /&gt;XXXX,D,2-May-02,82.47,85.76,82.05,83.84,7210000,&lt;br /&gt;XXXX,D,1-May-02,86.80,90.83,81.74,85.50,14253300&lt;br /&gt;&lt;br /&gt;"1997",car model,E350&lt;br /&gt;1997,car model,E350,"  Super luxurious truck    "&lt;br /&gt;1997,car model,E350,"Go get one now&lt;br /&gt;they are going fast"&lt;br /&gt;1997,car model,E350,"Super ""luxurious"" truck"&lt;br /&gt;1997,car model,E350,"Super, luxurious truck"&lt;br /&gt;&lt;br /&gt;1997,car model,E350,"ac, abs, moon",3000.00&lt;br /&gt;1999, car model,"Venture ""Extended Edition""",,4900.00,&lt;br /&gt;1996, car model,Old Car,"BEYOND REPAIR!&lt;br /&gt;air, moon roof, loaded",4799.00&lt;br /&gt;&lt;br /&gt;This,is,a test,CSV, file," from ""http://lorance.freeshell.org/csv/test.csv""."&lt;br /&gt;It contains,"quoted text",and,numbers 1234,5678&lt;br /&gt;It also has,"quoted text with an embedded quote""&lt;- right there"&lt;br /&gt;Then there are a few,,blank fields like these here -&gt;,,,&lt;br /&gt;A quoted blank field,"",&lt;- there.&lt;br /&gt;A quoted blank field with newline,"\n",&lt;- there.&lt;br /&gt;This next one causes an error if newline handling is turned off.&lt;br /&gt;"There is a newline here -&gt;&lt;br /&gt;&lt;- and it should be processed correctly."&lt;br /&gt;ABCD&lt;br /&gt;"And here,,, is an""Error - no"&lt;br /&gt;"And here,,, is an"Error - yes&lt;br /&gt;"And here,,, is an",Error - no&lt;br /&gt;&lt;br /&gt;1,2,3&lt;br /&gt;ab,"c,d","e""f", "g"",""","h&lt;br /&gt;jk",kl&lt;br /&gt;&lt;br /&gt;"aaa","bbb","ccc"&lt;br /&gt;zzz,yyy,xxx&lt;br /&gt;"aaa","b&lt;br /&gt;bb","ccc"&lt;br /&gt;zzz,yyy,xxx&lt;br /&gt;&lt;br /&gt;"aaa","b""bb","ccc"&lt;br /&gt;&lt;br /&gt;EOS&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;csv_data.split(/(,|\r\n|\n|\r)(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/m).each do |csv|&lt;br /&gt;#csv_data.split(/[,\n\r]+(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/m).each do |csv|&lt;br /&gt;&lt;br /&gt;   next if csv.empty?&lt;br /&gt;&lt;br /&gt;   csv = csv.strip&lt;br /&gt;&lt;br /&gt;   if csv =~ /\A(".*[^"]|[^"].*")\z/m then     # examples: csv =&gt; "ab\nc"def  or  abc"de\nf"&lt;br /&gt;       puts&lt;br /&gt;       puts "Error:" &lt;br /&gt;       p csv &lt;br /&gt;       puts csv[/\A./mu], csv[/.\z/mu] &lt;br /&gt;       #puts csv[0..0], csv[-1..-1] &lt;br /&gt;       puts&lt;br /&gt;       next&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   if csv =~ /\A".*"\z/m then csv.gsub!(/\A"(.*)"\z/m, '\1') end  # remove double-quotes at string beginning &amp; end&lt;br /&gt;   if csv =~ /""/m then csv.gsub!(/""/m, '"') end                 # remove a double-quote from double double-quotes&lt;br /&gt;&lt;br /&gt;   p csv&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 18 Aug 2007 17:17:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4430</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>PHP : Exportar CSV a mySQL / Export CSV to mySQL</title>
      <link>http://snippets.dzone.com/posts/show/4344</link>
      <description>C&#243;odigo fuente / Source code :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function exportarCSV_a_mySQL($fileCSV)&lt;br /&gt;{&lt;br /&gt;	$registros=0;&lt;br /&gt;&lt;br /&gt;	$ruta=$fileCSV['tmp_name'];&lt;br /&gt;&lt;br /&gt;	if(!file_exists($ruta))&lt;br /&gt;	{return false;}&lt;br /&gt;&lt;br /&gt;	$tabla=quitar_extension($fileCSV['name']);&lt;br /&gt;	&lt;br /&gt;	$borra_tabla="DROP TABLE `".$tabla."`";&lt;br /&gt;	mysql_query($borra_tabla);&lt;br /&gt;	$f=fopen($ruta,"r");&lt;br /&gt;	if($f)&lt;br /&gt;	{&lt;br /&gt;		echo "&lt;b&gt;Guardando CSV en la BDD :&lt;/b&gt;&lt;br /&gt;";&lt;br /&gt;		$contenido=fread($f,filesize($ruta));&lt;br /&gt;		fclose($f);&lt;br /&gt;		$contenido=ereg_replace("\r\n", "\n" , $contenido); // convertimos windows a unix&lt;br /&gt;		$lineas=explode("\n",$contenido);&lt;br /&gt;		$titulo=explode(";",$lineas[0]);&lt;br /&gt;		$NUM_CAMPOS=count($titulo);&lt;br /&gt;		$sql_generado_para_eliminar="";&lt;br /&gt;		$crear_tabla_campos="";&lt;br /&gt;		for($i=0;$i&lt;$NUM_CAMPOS;$i++)&lt;br /&gt;		{&lt;br /&gt;		$titulo[$i]=ereg_replace("\"", "" , $titulo[$i]); // kitamos comillas&lt;br /&gt;		$sql_generado_para_eliminar.=" AND `".$titulo[$i]."` =''";&lt;br /&gt;		$crear_tabla_campos.="`".$titulo[$i]."` varchar(60) NOT NULL";&lt;br /&gt;			if($i+1!=$NUM_CAMPOS)// si no es el ultimo , ponemos coma&lt;br /&gt;			{&lt;br /&gt;			$crear_tabla_campos.=",";&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		$crear_tabla="CREATE TABLE `".$tabla."` (".$crear_tabla_campos.") ENGINE=MyISAM DEFAULT CHARSET=latin1;";&lt;br /&gt;		mysql_query($crear_tabla);&lt;br /&gt;		$linea=1;&lt;br /&gt;		do&lt;br /&gt;		{&lt;br /&gt;			$insertar_titulos="";&lt;br /&gt;			$insertar_campos="";&lt;br /&gt;			$campo=explode(";",$lineas[$linea]);&lt;br /&gt;			for($i=0;$i&lt;$NUM_CAMPOS;$i++)&lt;br /&gt;			{&lt;br /&gt;			$campo[$i]=ereg_replace("\"", "" , $campo[$i]);&lt;br /&gt;			$insertar_titulos.=" `".$titulo[$i]."` ";&lt;br /&gt;			$insertar_campos.=" '".$campo[$i]."' ";&lt;br /&gt;				if($i+1!=$NUM_CAMPOS)// si no es el ultimo , ponemos coma&lt;br /&gt;				{&lt;br /&gt;				$insertar_titulos.=",";&lt;br /&gt;				$insertar_campos.=",";&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			$sql="INSERT INTO `".$tabla."` ( ".$insertar_titulos." ) VALUES ( ".$insertar_campos." );";&lt;br /&gt;			if(mysql_query($sql))&lt;br /&gt;			{&lt;br /&gt;			echo ". ";&lt;br /&gt;			$registros++;&lt;br /&gt;			}&lt;br /&gt;			else&lt;br /&gt;			{echo "X ";return false;}&lt;br /&gt;		$linea++;&lt;br /&gt;		}while(next($lineas));&lt;br /&gt;&lt;br /&gt;	$sql="DELETE FROM `".$tabla."` WHERE 1".$sql_generado_para_eliminar;mysql_query($sql);&lt;br /&gt;	echo "&lt;br /&gt;";&lt;br /&gt;	return $tabla;&lt;br /&gt;	}&lt;br /&gt;	else&lt;br /&gt;	{&lt;br /&gt;	return false;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function quitar_extension($archivo)&lt;br /&gt;{&lt;br /&gt;	$extension = strrchr($archivo,".");&lt;br /&gt;	$pos=strpos($archivo,$extension);&lt;br /&gt;	return substr($archivo,0,$pos);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;C&#243;digo ejemplo de llamada / Code exmple call :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$tabla = exportarCSV_a_mySQL($_FILES['archivo_csv']);&lt;br /&gt;if($tabla)&lt;br /&gt;{&lt;br /&gt;echo "Export OK in mysql table : ".$tabla;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;echo "Error in export ...";&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 23 Jul 2007 10:39:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4344</guid>
      <author>Ricardo (Ricardo m. Garc&#237;a)</author>
    </item>
    <item>
      <title>Turn CSV with headers into Array of Hashes (in 5 lines or less)</title>
      <link>http://snippets.dzone.com/posts/show/3899</link>
      <description>This assumes you have a CSV file whose first line are headings/labels for the individual columns.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'csv'&lt;br /&gt;&lt;br /&gt;csv_data = CSV.read 'data.csv'&lt;br /&gt;headers = csv_data.shift.map {|i| i.to_s }&lt;br /&gt;string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }&lt;br /&gt;array_of_hashes = string_data.map {|row| Hash[*headers.zip(row).flatten] }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Apr 2007 15:10:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3899</guid>
      <author>seancribbs (Sean Cribbs)</author>
    </item>
    <item>
      <title>Ruby CSV to XML Converter</title>
      <link>http://snippets.dzone.com/posts/show/3701</link>
      <description>This code will take an input CSV file and output XML. IT was easy to write, but I haven't found anything out there to do this. The first line of the CSV file should contain the element names.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;require 'csv'&lt;br /&gt;&lt;br /&gt;print "CSV file to read: "&lt;br /&gt;input_file = gets.chomp&lt;br /&gt;&lt;br /&gt;print "File to write XML to: "&lt;br /&gt;output_file = gets.chomp&lt;br /&gt;&lt;br /&gt;print "What to call each record: "&lt;br /&gt;record_name = gets.chomp&lt;br /&gt;&lt;br /&gt;csv = CSV::parse(File.open(input_file) {|f| f.read} )&lt;br /&gt;fields = csv.shift&lt;br /&gt;&lt;br /&gt;puts "Writing XML..."&lt;br /&gt;&lt;br /&gt;File.open(output_file, 'w') do |f|&lt;br /&gt;  f.puts '&lt;?xml version="1.0"?&gt;'&lt;br /&gt;  f.puts '&lt;records&gt;'&lt;br /&gt;  csv.each do |record|&lt;br /&gt;    f.puts " &lt;#{record_name}&gt;"&lt;br /&gt;    for i in 0..(fields.length - 1)&lt;br /&gt;      f.puts "  &lt;#{fields[i]}&gt;#{record[i]}&lt;/#{fields[i]}&gt;"&lt;br /&gt;    end&lt;br /&gt;    f.puts " &lt;/#{record_name}&gt;"&lt;br /&gt;  end&lt;br /&gt;  f.puts '&lt;/records&gt;'&lt;br /&gt;end # End file block - close file&lt;br /&gt;&lt;br /&gt;puts "Contents of #{input_file} written as XML to #{output_file}."&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 19 Mar 2007 19:17:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3701</guid>
      <author>jnorris (Jason Norris)</author>
    </item>
    <item>
      <title>Load CSV data into a database (Scriptella ETL tool)</title>
      <link>http://snippets.dzone.com/posts/show/3508</link>
      <description>This example demonstrates usage of &lt;a href="http://scriptella.javaforge.com"&gt;Scriptella ETL Tool&lt;/a&gt; to load CSV data into a database table.&lt;br /&gt;&lt;br /&gt;Input CSV file data.csv:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;id,priority,summary,status&lt;br /&gt;1,Critical,NullPointerException in Main class,Open&lt;br /&gt;5,Low,"Checkstyle, PMD, Findbugs issues",Reopened&lt;br /&gt;7,Low,Maven integration,Open&lt;br /&gt;10,High,SPI API,Closed&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;The CSV loading script has the following content:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd"&gt;&lt;br /&gt;&lt;etl&gt;&lt;br /&gt;  &lt;connection id="in" driver="csv" url="data.csv"/&gt;&lt;br /&gt;  &lt;connection id="out" driver="oracle" url="jdbc:oracle:thin:@localhost:1521:ORCL" &lt;br /&gt;      classpath="ojdbc14.jar" user="scott" password="tiger"/&gt;&lt;br /&gt;  &lt;!-- Copy all CSV rows to a database table --&gt;&lt;br /&gt;  &lt;query connection-id="in"&gt;&lt;br /&gt;      &lt;!-- Empty query means select all columns --&gt;&lt;br /&gt;      &lt;script connection-id="out"&gt;&lt;br /&gt;          INSERT INTO Table_Name VALUES (?id,?priority, ?summary, ?status)&lt;br /&gt;      &lt;/script&gt;&lt;br /&gt;  &lt;/query&gt;&lt;br /&gt;&lt;/etl&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use RegEx to filter CSV data:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;query connection-id="in"&gt;&lt;br /&gt;    &lt;!--Select bugs with status open or reopened.--&gt;&lt;br /&gt;    ,,,open|reopened&lt;br /&gt;    &lt;!--Inserts imported rows into a database--&gt;&lt;br /&gt;    &lt;script connection-id="out"&gt;&lt;br /&gt;       INSERT INTO Table_Name VALUES (?id, ?priority, ?summary, ?status);&lt;br /&gt;    &lt;/script&gt;&lt;br /&gt;&lt;/query&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 13 Feb 2007 09:42:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3508</guid>
      <author>ejboy (Fyodor Kupolov)</author>
    </item>
    <item>
      <title>CSV Parser / Writer for PHP</title>
      <link>http://snippets.dzone.com/posts/show/3128</link>
      <description>CSV Parser / Writer&lt;br /&gt;&lt;br /&gt;Example A:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//cell separator, row separator, value enclosure&lt;br /&gt;$csv = new CSV(';', "\r\n", '"');&lt;br /&gt;&lt;br /&gt;//parse the string content&lt;br /&gt;$csv-&gt;setContent(file_get_contents('data.csv'));&lt;br /&gt;&lt;br /&gt;//returns an array with the CSV data&lt;br /&gt;print_r($csv-&gt;getArray());&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Exemple B:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$csv = new CSV(';', "\r\n", '"');&lt;br /&gt;//sets up the content through an array&lt;br /&gt;$csv-&gt;setArray(&lt;br /&gt;	array(&lt;br /&gt;		array('col"una1', "colu\r\nna2"),&lt;br /&gt;		array('col;una3', 'coluna4')&lt;br /&gt;	)&lt;br /&gt;);&lt;br /&gt;//retorns string with the CSV representation&lt;br /&gt;print $csv-&gt;getContent();&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com&lt;br /&gt;class CSV{&lt;br /&gt;	var $cellDelimiter;&lt;br /&gt;	var $valueEnclosure;&lt;br /&gt;	var $rowDelimiter;&lt;br /&gt;&lt;br /&gt;	function CSV($cellDelimiter, $rowDelimiter, $valueEnclosure){&lt;br /&gt;		$this-&gt;cellDelimiter = $cellDelimiter;&lt;br /&gt;		$this-&gt;valueEnclosure = $valueEnclosure;&lt;br /&gt;		$this-&gt;rowDelimiter = $rowDelimiter;&lt;br /&gt;		$this-&gt;o = array();&lt;br /&gt;	}&lt;br /&gt;	function getArray(){&lt;br /&gt;		return $this-&gt;o;&lt;br /&gt;	}&lt;br /&gt;	function setArray($o){&lt;br /&gt;		$this-&gt;o = $o;&lt;br /&gt;	}&lt;br /&gt;	function getContent(){&lt;br /&gt;		if(!(($bl = strlen($b = $this-&gt;rowDelimiter)) &amp;&amp; ($dl = strlen($d = $this-&gt;cellDelimiter)) &amp;&amp; ($ql = strlen($q = $this-&gt;valueEnclosure))))&lt;br /&gt;			return '';&lt;br /&gt;		for($o = $this-&gt;o, $i = -1; ++$i &lt; count($o);){&lt;br /&gt;			for($e = 0, $j = -1; ++$j &lt; count($o[$i]);)&lt;br /&gt;				(($e = strpos($o[$i][$j], $q) !== false) || strpos($o[$i][$j], $b) !== false || strpos($o[$i][$j], $d) !== false)&lt;br /&gt;				&amp;&amp; $o[$i][$j] = $q . ($e ? str_replace($q, $q . $q, $o[$i][$j]) : $o[$i][$j]) . $q;&lt;br /&gt;			$o[$i] = implode($d, $o[$i]);&lt;br /&gt;		}&lt;br /&gt;		return implode($b, $o);&lt;br /&gt;	}&lt;br /&gt;	function setContent($s){&lt;br /&gt;		$this-&gt;o = array();&lt;br /&gt;		if(!strlen($s))&lt;br /&gt;			return true;&lt;br /&gt;		if(!(($bl = strlen($b = $this-&gt;rowDelimiter)) &amp;&amp; ($dl = strlen($d = $this-&gt;cellDelimiter)) &amp;&amp; ($ql = strlen($q = $this-&gt;valueEnclosure))))&lt;br /&gt;			return false;&lt;br /&gt;		for($o = array(array('')), $this-&gt;o = &amp;$o, $e = $r = $c = 0, $i = -1, $l = strlen($s); ++$i &lt; $l;){&lt;br /&gt;			if(!$e &amp;&amp; substr($s, $i, $bl) == $b){&lt;br /&gt;				$o[++$r][$c = 0] = '';&lt;br /&gt;				$i += $bl - 1;&lt;br /&gt;			}&lt;br /&gt;			elseif(substr($s, $i, $ql) == $q){&lt;br /&gt;				$e ? (substr($s, $i + $ql, $ql) == $q ?&lt;br /&gt;				$o[$r][$c] .= substr($s, $i += $ql, $ql) : $e = 0)&lt;br /&gt;				: (strlen($o[$r][$c]) == 0 ? $e = 1 : $o[$r][$c] .= substr($s, $i, $ql));&lt;br /&gt;				$i += $ql - 1;&lt;br /&gt;			}&lt;br /&gt;			elseif(!$e &amp;&amp; substr($s, $i, $dl) == $d){&lt;br /&gt;				$o[$r][++$c] = '';&lt;br /&gt;				$i += $dl - 1;&lt;br /&gt;			}&lt;br /&gt;			else&lt;br /&gt;				$o[$r][$c] .= $s[$i];&lt;br /&gt;		}&lt;br /&gt;		return true;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 11 Dec 2006 22:36:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3128</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>Snippet to grab historical data for stocks</title>
      <link>http://snippets.dzone.com/posts/show/2350</link>
      <description>This is a quick little snippet I'm whipping up to import some historical data in for a graphing app we're building for stock data.  I figured I'd post this snippet before I maul it into something application-specific...&lt;br /&gt;&lt;br /&gt;It downloads a csv file through yahoo's finance site and then parses it and prints out the date and adjusted close for each business day that has data.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'open-uri'&lt;br /&gt;require 'csv'&lt;br /&gt;&lt;br /&gt;def get_adjusted_close stock_symbol&lt;br /&gt;  puts "-- #{stock_symbol} Adjusted Close - Historical --"&lt;br /&gt;  url = "http://ichart.finance.yahoo.com/table.csv?s=#{stock_symbol}&amp;d=7&amp;e=1&amp;f=2006&amp;g=d&amp;a=2&amp;b=26&amp;c=1990&amp;ignore=.csv"&lt;br /&gt;  puts "Connecting to #{url}\n"&lt;br /&gt;&lt;br /&gt;  csv = CSV.parse(open(url).read)&lt;br /&gt;&lt;br /&gt;  csv.each{|row|&lt;br /&gt;    puts "#{row[0]} - #{row.last}"&lt;br /&gt;  }&lt;br /&gt;  puts "---------------------------------"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;example_stocks = "CSCO GOOG"&lt;br /&gt;print "Enter a series of stock symbols separated by spaces (example: #{example_stocks}) to retrieve the historical adjusted close.\n"&lt;br /&gt;stock_symbols = gets&lt;br /&gt;stock_symbols ||= example_stocks&lt;br /&gt;&lt;br /&gt;stock_symbols.split.each{|symbol|&lt;br /&gt;  get_adjusted_close(symbol)&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 01 Aug 2006 21:46:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2350</guid>
      <author>jadams (Josh Adams)</author>
    </item>
    <item>
      <title>Make a list into a line suitable for a CSV file in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2047</link>
      <description>Formats a list/array as a string suitable for one row of a CSV file&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def comma_separate(items)&lt;br /&gt;  items.map! do |item|&lt;br /&gt;    if item.is_a?(String) and item =~ /[",]/&lt;br /&gt;      '"' + item.gsub(/"/, '""') + '"'&lt;br /&gt;    else&lt;br /&gt;      item&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  items.join(',')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 23:45:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2047</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
  </channel>
</rss>
