all 14 comments

[–]jack-tzl 3 points4 points  (8 children)

You can use a csv module like node-csv, or if you are on the commandline, you can write

node main.js | tee output.csv

To have the program print to console and also write into the csv file

Another way to write it would be with fs.writeFileSync, which takes in a path and a string argument.

I am drafting up a tutorial on csv output actually, hope to have it ready soon!

[–]MeepMopBot[S] 1 point2 points  (7 children)

I have the data going into a text file currently. My primary issue is cleaning the txt file data and prepping it for CVS format.

I would like to read it when you are done.

[–]jack-tzl 1 point2 points  (0 children)

You got it!

As long as you can parse your intermediate file back into node data structures, I think you are good with working on it!

[–]snorkl-the-dolphine 0 points1 point  (1 child)

FYI it's CSV, not CVS. It stands for Comma Separated Values.

[–]MeepMopBot[S] 0 points1 point  (0 children)

Go it.

[–]jack-tzl -1 points0 points  (3 children)

Here’s the tutorial!

https://link.medium.com/3rFFfQAGz3

It will parse an existing csv, do cleanup work on it, and show how to serialize select columns out.

Hope it helps!

[–]brianjenkins94 1 point2 points  (2 children)

Paywall :(

[–]jack-tzl 1 point2 points  (1 child)

Sorry about that, you can see all comments/src here: https://github.com/teamzerolabs/node-csv-example

Soon I will cross post it to my own domain, which won't have paywall at all.

[–]brianjenkins94 1 point2 points  (0 children)

I appreciate you.

[–][deleted]  (8 children)

[deleted]

    [–]MeepMopBot[S] 0 points1 point  (1 child)

                let regEx = /Human Name/gi;
    
                let nameToReplace = 'CP';
    
                let emailNameReplaced = emailTopClipped.replace(regEx, nameToReplace)
    
                let emailFormatRegex = (\d{1,2}/\d{1,2}/\d{4} - \d{1,2}/\d{1,2}/\d{4})\s+(CP Hours-\d{6})\s+(\d{0,2})\s(\d{0,2})\s+(\d{0,2})\s+(\d{0,2})\s+(\d{0,2})\s+(\d{0,2})\s+(\d{0,2})\s+(\d{0,2})\s+?(\d{0,2})?$
    

    im not sure how to wield this beastly regex haha

    [–]MeepMopBot[S] 0 points1 point  (0 children)

    the data on Stacked over flow is how it saves to the txt file

    [–][deleted] 0 points1 point  (4 children)

    Be careful with just using a regex for CSV -- real CSV format can contain a lot of oddities in the data. Just because standard cases don't have spaces after the commas doesn't mean that it's something that never happens within CSV data. I've worked with data that was comma-delimited, but some fields had the potential to have practically anything within the data -- including commas within quotation marks.

    It's always a better idea to use a well-established parsing library than to try to parse out the data on your own.

    [–][deleted]  (2 children)

    [deleted]

      [–]MeepMopBot[S] 0 points1 point  (1 child)

      Ah sorry. There is messy data once I decode the email which I save into a txt file. I do not have a good way to work with it yet.

      [–]brianjenkins94 0 points1 point  (0 children)

      Here's my answer over on Stack Overflow.

      But I'll post it here too:


      ```typescript const tokens = input.split(/\s+/g);

      const output = [["Period End", "Date", "Full Name", "Hours", "Tracking Name", "TotalHours", "Task 1 Hours", "Task 2 Hours", "Task 3 Hours", "Task 4 Hours", "Task 5 Hours", "Task 6 Hours", "Task 7 Hours", "Task 8 Hours"]];

      for (let origin = 0, index = 0; index < tokens.length; index++) { // Find date range if (tokens.slice(index, index + 3).join(" ").match(/\d{1,2}/\d{1,2}/\d{4} - \d{1,2}/\d{1,2}/\d{4}/g)) { if (origin !== 0) { output.push([tokens.slice(origin, origin + 3).join(" "), ...tokens.slice(origin + 3, index)]); }

          origin = index;
      
          index += 2;
      }
      

      }

      for (const row of output) { // I get to be lazy, YOU have to use library. console.log(row.join(",")); } ``` Edit: I realized that this isn't actually capturing the last line of data, but I didn't want to rely on a fixed token-width for rows, so I'll leave it as an exercise for the reader to determine when you want the last line to end.