# Import txt to csv with JavaScript

**URL:** https://forum.kirupa.com/t/import-txt-to-csv-with-javascript/640158
**Category:** programming
**Created:** [August 9, 2019, 1:43am UTC](https://forum.kirupa.com/t/import-txt-to-csv-with-javascript/640158 "2019-08-09T01:43:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Qbaqba1](https://avatars.discourse-cdn.com/v4/letter/q/e68b1a/32.png) [@Qbaqba1](https://forum.kirupa.com/u/Qbaqba1)
#### Post date: [August 9, 2019, 1:43am UTC](https://forum.kirupa.com/t/import-txt-to-csv-with-javascript/640158/1 "2019-08-09T01:43:15Z")

</div>

Hello,

Im having a problem with a given task.  
The task is to write a JavaScript code that would transfer data from a txt file looking like this:  
14  
151  
1223  
12415  
13  
into a CSV file with “0” added before the number so all have total of 8 digits. I was planing to do it with padStart() method but besides that im pretty much clueless ☹

How am I supposed to import local txt file into JS while doing operations on the txt and save it as csv?

Eddit:  
as for now my code looks something like this:

//-------grab a file -------  
const input = document.querySelector(‘input[type=“file”]’)  
input.addEventListener(‘change’, function (e) {  
return input.files},false)  
//--------save as CSV--------  
function convertArrayOfObjectsToCSV(args) {  
var result, ctr, keys, columnDelimiter, lineDelimiter, data;

```
    data = args.data || null;
    if (data == null || !data.length) {
        return null;
    }

    columnDelimiter = args.columnDelimiter || ',';
    lineDelimiter = args.lineDelimiter || '\n';

    keys = Object.keys(data[0]);

    result = '';
    result += keys.join(columnDelimiter);
    result += lineDelimiter;

    data.forEach(function(item) {
        ctr = 0;
        keys.forEach(function(key) {
            if (ctr > 0) result += columnDelimiter;

            result += item[key];
            ctr++;
        });
        result += lineDelimiter;
    });

    return result;
}
    function downloadCSV(args) {  
    var data, filename, link;
    var csv = convertArrayOfObjectsToCSV({
        data: input
    });
    if (csv == null) return;

    filename = args.filename || 'export.csv';

    if (!csv.match(/^data:text\/csv/i)) {
        csv = 'data:text/csv;charset=utf-8,' + csv;
    }
    data = encodeURI(csv);

    link = document.createElement('a');
    link.setAttribute('href', data);
    link.setAttribute('download', filename);
    link.click();
}

```

However the first part of the code where is const input doesnt seem to work as intendeed. The 2nd part which is supposed to save given txt worked well when the data (data: input) was given with a var like var input = [“1”,“2”,“3”].

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [August 9, 2019, 5:18am UTC](https://forum.kirupa.com/t/import-txt-to-csv-with-javascript/640158/2 "2019-08-09T05:18:43Z")

</div>

Hey @Qbaqba1- welcome to the forums! Quite the fun question 😛

For the text file, can it be something you can browse to via the app? Or does the path have to be programmatically specified in the code?

---

<div class="post-metadata">

### Author: ![Qbaqba1](https://avatars.discourse-cdn.com/v4/letter/q/e68b1a/32.png) [@Qbaqba1](https://forum.kirupa.com/u/Qbaqba1)
#### Post date: [August 9, 2019, 9:49am UTC](https://forum.kirupa.com/t/import-txt-to-csv-with-javascript/640158/3 "2019-08-09T09:49:13Z")

</div>

It doesnt have to be spedified in the code as long as it gets job done 🙂

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [August 9, 2019, 6:46pm UTC](https://forum.kirupa.com/t/import-txt-to-csv-with-javascript/640158/4 "2019-08-09T18:46:39Z")

</div>

Perfect! This article should help you with loading the file and reading it: [https://www.html5rocks.com/en/tutorials/file/dndfiles/](https://www.html5rocks.com/en/tutorials/file/dndfiles/)

For saving the file, you can provide it as a download. This example should help: [http://jsfiddle.net/UselessCode/qm5AG/](http://jsfiddle.net/UselessCode/qm5AG/)

🙂
