0

I have a python script that is meant to read and write a csv locally. It also counts an average value of the number of lines. Now I need to translate it in JS native. But I don not even succeed to display it in an array. The array stays empty and I don't have any clue why.

The CSV I use look like:

outbound;API1;Restitution;1.0
outbound;API1;Restitution;1.0
outbound;API1;Operations;1.0
outbound;API2;Operations;2.0
outbound;API2;Operations;2.0
outbound;API3;Service;1.0

And when I translate it with my python script I obtain something like :

API1,0.50
API2,0.33333
API3,0.16666

The python script I want to translate:

import csv
from glob import glob

myDict = {}
filename = glob('*.csv')[0]     #take any .csv file
row_number = 0

with open(filename, 'rU') as f:     #arg 'r' for reading and 'U' for Universal, can read .csv without quotes.
    reader = csv.reader(f, delimiter=';')
    for row in reader:
        row_number +=1
        if row[1] in myDict:
            myDict[row[1]] += 1
        else:
            myDict[row[1]] = 1

data = open("output.csv", "w")
w = csv.writer(data)
for word in myDict:
    data = word, float(myDict[word])/row_number*100
    w.writerow(data)

my JS function :

var a_api = new Array();
            $.get('/data/dataTest2.csv', function(data) {
              let rows = data.split("\n");
              rows.forEach( function getvalues(thisRow) {
                let columns = thisRow.split(";");
                for (var i=0;i<a_api.length;i++){
                  if(columns[1]==a_api[i]){
                    a_api[i][1] +=1;
                  }
                  else {
                    a_api[i][0]=columns[1];
                    a_api[i][1]=1;
                  }
                }
              })
               });
console.log(a_api);
SandaleRaclette
  • 119
  • 1
  • 12
  • 1
    You don't show where you're checking the array, but with 99% certainty: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – JJJ Nov 07 '17 at 17:08
  • To get more accurate answers and comments, include the code where `a_api` is defined. – Sami Hult Nov 07 '17 at 17:11
  • Ok I updated my post but I define a_api just before the function and check it just after for now – SandaleRaclette Nov 07 '17 at 21:44

0 Answers0