[D3.js pro series-1.0] Reading CSV table files, d3.jscsv

Source: Internet
Author: User

[D3.js pro series-1.0] Reading CSV table files, d3.jscsv

In the tutorials of the Getting Started series, we often use d3.json () functions to read files in json format. The json format is very powerful, but it may not be suitable for common users. Common users prefer table files generated using Microsoft Excel or OpenOffice Calc, which are easy to edit and easy to understand.

Microsoft Excel is usually saved in the xls format, while OpenOffice Calc is usually saved in the ods format. These formats are powerful as table files, but it is troublesome to read them. This method is not provided in D3. However, table software supports generating csv files, which are basic, common, and simple table files. This article describes how to read and use csv files in D3.


1. What is the CSV format?

CSV (Comma Separated Values), Separated by commas (,). It stores table data in plain text format, and each cell is Separated by commas. The CSV format does not have a general standard and is generally described in RFC 4180.

The CSV text format is as follows:

Province, population, GDP Shandong, Zhejiang

It is easy to understand. Each cell is separated by a comma. What if I want to enter a comma in the cell? Use double quotation marks to box up, as shown below:

Province, population, GDP Shandong, "9,000", "50,000" Zhejiang, "5,000", "20,000"

When saving the CSV format, some software will ask you to use which symbols (such as commas and semicolons) to separate cells. Select commas as much as possible.

2. Edit and save the CSV file in OpenOffice

Although Microsoft Excel is powerful, it is charged and I have not used it in recent years. OpenOffice is not only open-source and free, but also powerful. Next, let's take a look at how to use OpenOffice to edit and save it as a CSV file. Of course, you can also use it if you are exploring it yourself, which is very simple.

(1) first, open OpenOffice Calc. Just as Microsoft Office has Word, Excel, and PowerPoint, editing in OpenOffice indicates that Calc is used. After opening, enter the content of the cell as normal. Assume that the input is as follows:

(2) Click "file" and "Save ". Select "CSV text" for the file type, and then select "Edit filter settings" under ".

(3) In the displayed dialog box, select encoding (UTF8 is recommended), "comma" as the field separator, and "Semicolon" as the text separator ". Click "OK ".

(4) After the file is saved, open it in Notepad. The result is as follows:

In D3.js, the function for reading CSV files only supports separating cells with commas, so be sure to save them like this.

3. Read CSV files in D3.js

D3.csv () function is provided in D3.js to read CSV files. For function APIs, see https://github.com/mbostock/d3/wiki/CSV.

The code used to read files is as follows:

d3.csv("table.csv",function(error,csvdata){if(error){console.log(error);}console.log(csvdata);});

This Code reads the table.csv file and then outputs the read data. The output is as follows:

We can see that csvdata in the variable stores an array. Each element in the array has an object, and each object has three member variables: age, name, and sex. These three member variables are the three cells in the first row of the edited table. In this way, we can call it in the code.

for( var i=0; i<csvdata.length; i++ ){var name = csvdata[i].name;var sex = csvdata[i].sex;var age = csvdata[i].age;console.log( "name: " + name + "\n" + "sex: " + sex + "\n" + "age: " + age  );}
4. Conclusion

In D3, there are still some functions for reading CSV files, which will be described in the following articles. Writing too much content at a time cannot be used.

Thank you for reading.

Document Information

  • Copyright Disclaimer: BY-non-commercial (NC)-deduction prohibited (ND)
  • Published on: February 1, October 03, 2014
  • More content: OUR D3.JS-data visualization special site and CSDN personal blog
  • Note: Please indicate the source for reprinting. Thank you.

Use of labview to read workbook files vi

The format of your file is CSV, and CSV is separated by commas (,). The default delimiter for reading a workbook file is Tab. to read a CSV file, replace the delimiter input with a comma (,).

How to use c # To Read *** csv table files and make calculations between columns

There are many methods to operate CSV, and they are not complicated.

The most common is to directly process text files, read them row by row, and Split them. This is really not difficult.

Second, you can use the Jet engine as a database file.
String constr = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = D :\\ csv \\; Extended Properties = \ "text; HDR = No; FMT = Delimited \" "; System. data. oleDb. oleDbConnection con = new System. data. oleDb. oleDbConnection (constr); con. open (); System. data. oleDb. oleDbDataAdapter adapter = new System. data. oleDb. oleDbDataAdapter ("select * from [test # csv]", con); DataTable dt = new DataTable (); adapter. fill (dt); con. close (); this. dataGridView1.DataSource = dt;
Alternatively, you can use a third-party tool, the most famous of which is CSVReader. One search is available.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.