Andrey
Andrey

Reputation: 73

d3: can't import text file with semicolon delimiter

I'm beginner at JavaScript and at D3 library. There are description for "d3-fetch" module (https://github.com/d3/d3-fetch) and for d3.dsv command API (https://github.com/d3/d3-dsv). I can't figure out from these two topics how to make the import with non standard delimiter - semicolon i.e.: dsv parser receives a string as an input. So how to feed it with the whole file?

I typed the non working code, would be grateful for any advises:

<body>
    <script type="text/javascript">
        var scsv = d3.dsvFormat(";");

        d3.text("$lab/raw.csv").then(function(raw) {
            scsv.parse(raw).then(function(d) {
                    console.log(d);
            });
        });
    </script>
</body>

Upvotes: 0

Views: 1180

Answers (1)

Kosh
Kosh

Reputation: 18423

As per documentation:

// set `doc` to your actual url
var doc = 'data:text/plain;base64,' + btoa('cat;dog\n1;0');

d3.dsv(";", doc).then(function(data) {
  console.log(data);
});
<script src="https://d3js.org/d3-dsv.v1.min.js"></script>
<script src="https://d3js.org/d3-fetch.v1.min.js"></script>

Upvotes: 1

Related Questions