DMelt:IO/4 CSV File Format

From HandWiki
Jump to: navigation, search
Limitted access. First login to DataMelt if you are a full DataMelt member. Then login to HandWiki as a user.
Table of contents

CSV file format

Cross platform I/O can be also achieved using Comma-separated_values (CVS) files. You can use this format to fill spreadsheets and read them in spreadsheet programs ( Calc, Excel, Gnumeric, StarCalc). The format is a text file, encoded in ASCII, with a header and body. The Java class for this format is called

Here is a simple Python example showing how to create a CSV file

from jhplot import *
from import *

p=P1D("X-Y values with error 0.5 on Y")


The output file "test.csv" can be read in OpenOffice or any other spreadsheet programs.

Here is a more complicated example:

from java.util import Random
from jhplot  import *
from  import *

h1 = H1D("Histogram",20, -2.0, 2.0)
rand = Random()
for i in range(100):

This example writes histogram to a CSV file which then can be opened in other programs.

To read CVS files, use the "" package. Here is a little example how to do this in JAVA:

import java.util.*;

String ADDRESS_FILE="examples/addresses.csv";
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
   System.out.println("Name: [" + nextLine[0] + "]\nAddress: [" + nextLine[1] + "]\nEmail: [" + nextLine[2] + "]");

'' Try writing it back out as CSV to the console
CSVReader reader2 = new CSVReader(new FileReader(ADDRESS_FILE));
List<String[]> allElements = reader2.readAll();
StringWriter sw = new StringWriter();
CSVWriter writer = new CSVWriter(sw);
System.out.println("\n\nGenerated CSV File:\n\n");

You might just want to slurp the whole lot into a List, just call readAll()...

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();

which will give you a List of String[] that you can iterate over. One can use a custom separators and quote characters. There are constructors that cater for supplying your own separator and quote characters. Say you're using a tab for your separator, you can do something like this:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');

And if you single quoted your escaped characters rather than double quote them, you can use the three arg constructor:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'');

You may also skip the first few lines of the file if you know that the content doesn't start till later in the file. So, for example, you can skip the first two lines by doing:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'', 2);

In order to write CSV files, there is a CSVWriter in the same package that follows the same semantics as the CSVReader. For example, to write a tab separated file:

CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
'' feed in your array (or convert your data to an array)
String[] entries = "first#second#third".split("#");

If you'd prefer to use your own quote characters, you may use the three arg version of the constructor, which takes a quote character (or feel free to pass in CSVWriter.NO_QUOTE_CHARACTER).

You can also customise the line terminators used in the generated file (which is handy when you're exporting from your Linux web application to Windows clients). There is a constructor argument for this purpose.

Now we will show how to create a P1D container in order to plot 2 first columns from CSV. This can be useful to further plot such data. Here is a small Jython code which illustrate how to read and create P1D:

from import *
from import *
from java.util import *
from jhplot import *

reader =CSVReader(FileReader("a.csv"));
p1=P1D("CSV file")
while True:
      nextLine = reader.readNext() 
      if nextLine== None: break
      p1.add( float(nextLine[0]), float(nextLine[1]))

ASCII, Gauss, Matlab, Excel data formats

DataMelt can read data (time series) in variety of formats, such as ASCII, Gauss, Matlab, Excel. Data can be modified, showed as tables/ One can plot such data and perform a statistical analysis. One can also save such data into files. Read Time series for detail since this topic is closely related to financial calculations