[D3.js advanced series-1.2] solution for garbled characters when reading CSV files, d3.jscsv
When d3.csv is used in D3 to read CSV files, garbled characters may occur. How can this problem be solved?
1. garbled Problem
When reading the xxx.csv file using d3.csv, if the xxx.csv file uses UTF-8 encoding, there is no problem. Of course, I personally think that the use of UTF-8 encoding, can be used in the same encoding of national text.
However, if the xxx.csv file is UTF-8 encoded, garbled characters may occur when it is opened in Microsoft Excel, because the Excel file in China is opened in GB2312 by default, in addition, you cannot select encoding when opening (OpenOffice does not have this problem ). Of course, there are methods in Excel that can be opened in UTF-8 encoding. Specific methods can be found on the Internet.
GB2312 and GB18030 are commonly used in China. If CSV files are stored in these two encoding formats, they can be opened directly in Excel without garbled characters. However, if you use d3.csv to read data, garbled characters may occur during visualization. This is the problem.
2. Solution
The solution is simple, but if you do not know it, it is very effort-consuming.
To solve this problem, you must manually set the encoding used to read the file in D3.
The d3.csv and d3.tsv functions are used in the [advanced series 1.0] and [advanced series 1.1]. The difference between them is that the separators are different. It is also mentioned that they are essentially d3.dsv functions, namely:
d3.csv = d3.dsv(",", "text/csv"); d3.tsv = d3.dsv("", "text/tab-separated-values");
In the second parameter of d3.dsv, you can add the encoding, for example:
var csv = d3.dsv(",", "text/csv;charset=gb2312");var tsv = d3.dsv("", "text/tab-separated-values;charset=gb2312");
In this way, you can define your own csv and tsv reading functions, and add the encoding above. The usage method is shown as follows:
csv("xxx.csv",function(error,csvdata){}tsv("xxx.tsv",function(error,tsvdata){}
In this way, the garbled problem can be solved. Thank you for reading.
Document Information
- Copyright Disclaimer: BY-non-commercial (NC)-deduction prohibited (ND)
- Published on: February 1, October 18, 2014
- More content: OUR D3.JS-data visualization special site and CSDN personal blog
- Note: Please indicate the source for reprinting. Thank you.
How can a csv file be compiled into a garbled dat format and the dat format file to be read during program use can be read
Csv is an ASCII file, so you can read meaningful content with the naked eye.
Data you write can be stored in binary files. It is difficult for others to read because they do not know your data format.
C language:
FILE * fout;
Fout = fopen ("abc. dat", "wb"); // open abc. dat and use the binary mode for output.
Next, use fwrite (...); to output data.
This type of file is opened with notepad and so on. It looks like "garbled ".
Other programming languages also have 2-digit statements.
How can I solve csv file garbled characters when I export yahoo Mail Addresses to outlook?
1. If you use outlook to receive yahoo Mail, you do not have to export its address.
2. CSV is not a format for you to open the browser, so garbled characters will appear, but the system will identify it by itself. Follow the prompts.
This supplement is irrelevant to the question you asked.
Supported