简体   繁体   English

d3 用逗号替换分号

[英]d3 replacing semicolon with comma

Hoping someone way clever than me can quickly help solve this.希望比我更聪明的人可以快速帮助解决这个问题。 I have csv files coming in with a semicolon seperator.我有带有分号分隔符的 csv 文件。 it cant read the headings:它无法阅读标题:

d3.csv("file.csv", function(error, data) {
   data.forEach(function(d) {
       d.date = parseDate(d.date);
       d.value2 = +d.value2;
       d.value1 = +d.value1;
   });

I've tried adding something like data.replace(/\\s*;\\s*/g, ",") but doesnt work.我试过添加类似 data.replace(/\\s*;\\s*/g, ",") 但不起作用。

Appreciate the help.感谢帮助。

Let's convert my comment to an answer, so it's not left unanswered:让我们将我的评论转换为答案,这样就不会没有答案了:

Assuming that your values are separated by a semicolon (so technically it's not a CSV anymore), you might want to look at this: https://github.com/d3/d3-dsv假设您的值由分号分隔(因此从技术上讲它不再是 CSV),您可能需要查看: https : //github.com/d3/d3-dsv

Old answer, check more recent answers旧答案,查看更多最近的答案

The following code works for me :以下代码对我有用:

d3.dsv(';')("your_file.csv", function(error, data){
   ...
}

d3.dsv(separator) is a contructor that returns a function that works exactly as the d3.csv function. d3.dsv(separator)是一个构造函数,它返回一个与d3.csv函数完全相同的函数。 Actually, d3.csv is equivalent to d3.dsv(',')实际上, d3.csv相当于d3.dsv(',')

Just for the precision, .csv with semi-colons are default in France for example (yeah after all, it is not as if csv meant "comma-separated values").只是为了精度,例如,带分号的.csv在法国是默认的(是的,毕竟,csv 的意思并不是“逗号分隔值”)。 DSV stands for delimiter-separated values and are more general. DSV代表分隔符分隔值,更通用。

D3 V3+ or Higher Working Example D3 V3+ 或更高版本的工作示例

<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
            table {
                border-collapse: collapse;
                border: 2px black solid;
                font: 12px sans-serif;
                font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
                width: 100%;
            }
        tr:first-child {
          padding-top: 12px;
          padding-bottom: 12px;
          text-align: left;
          background-color: #4CAF50;
          color: white;
          background-color: #4CAF50;
         }
      
         td {
            border: 1px black solid;
            padding: 5px;
            border: 1px solid #ddd;
            padding: 8px;
            }


         tr:nth-child(even){background-color: #f2f2f2;}

         tr:hover {background-color: #ddd;}

    </style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>

<script type="text/javascript" charset="utf-8">
            d3.text("data.csv", function(data) {
                var parsedCSV = d3.dsv(';').parseRows(data);

                var container = d3.select("body")
                    .append("table")

                    .selectAll("tr")
                        .data(parsedCSV).enter()
                        .append("tr")

                    .selectAll("td")
                        .data(function(d) { return d; }).enter()
                        .append("td")
                        .text(function(d) { return d; });
            });

</script>
</body>
</html>

Reference: http://bl.ocks.org/ndarville/7075823参考: http : //bl.ocks.org/ndarville/7075823

I have made the Table a bit styles and more readable.我已经使表格有点样式和更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM