简体   繁体   English

如何使用 Nodejs/JavaScript 将此数据转换为 csv

[英]How to convert this data to csv using Nodejs/ JavaScript

I have been having some trouble cleaning this data up using NodeJS so I can plug it into a CSV sheet.我在使用 NodeJS 清理这些数据时遇到了一些麻烦,因此我可以将其插入到 CSV 表中。 The numbers for hours are subject to change.小时数可能会发生变化。 The last two number rows are options as there are a total for each column, if removed though it would have to be with node.最后两个数字行是选项,因为每列都有一个总数,如果将其删除,尽管它必须与节点一起使用。

The below data is saved in a .txt file currently以下数据目前保存在一个 .txt 文件中

Apprentice Name  ↑ Period End Date  ↑ Full Name Hours Tracking Name Total
Hours Task 1 Hours Task 2 Hours Task 3 Hours Task 4 Hours Task 5 Hours Task
6 Hours Task 7 Hours Task 8 Hours
CP - Apprentice 4/21/2019 - 4/27/2019 CP
Hours-000104 40 15 10 0 0 0 15 0 0
  4/28/2019 - 5/4/2019 CP Hours-000103 40 10 10 10 0 0 10 0
0
  5/5/2019 - 5/11/2019 CP Hours-000118 40 10 10 10 0 10 0 0
0
  5/12/2019 - 5/18/2019 CP Hours-000233 40 10 10 10 10 0 0 0
0
  5/19/2019 - 5/25/2019 CP Hours-000164 40 10 10 10 10 0 0 0
0
  5/26/2019 - 6/1/2019 CP Hours-000210 40 0 0 0 10 10 10 10
0
  6/2/2019 - 6/8/2019 CP Hours-000211 40 5 20 5 5 0 5 0 0
  6/9/2019 - 6/15/2019 CP Hours-000234 40 10 10 10 0 10 0 0
0
  6/16/2019 - 6/22/2019 CP Hours-000251 40 0 30 10 0 0 0 0 0
  6/23/2019 - 6/29/2019 CP Hours-000292 40 10 30 0 0 0 0 0 0
  6/30/2019 - 7/6/2019 CP Hours-000310 40 0 40 0 0 0 0 0 0
  7/7/2019 - 7/13/2019 CP Hours-000455 40 0 30 0 10 0 0 0 0
  7/14/2019 - 7/20/2019 CP Hours-000745 40 0 10 10 10 10 0 0
0
  7/21/2019 - 7/27/2019 CP Hours-000709 40 0 0 10 20 10 0 0
0
  7/28/2019 - 8/3/2019 CP Hours-000708 40 0 20 0 0 0 10 10 0
  8/4/2019 - 8/10/2019 CP Hours-000851 40 0 0 0 40 0 0 0 0
  8/11/2019 - 8/17/2019 CP Hours-000886 40 0 0 0 40 0 0 0 0
  8/18/2019 - 8/24/2019 CP Hours-001133 40 0 0 0 40 0 0 0 0
  8/25/2019 - 8/31/2019 CP Hours-001311 40 0 0 0 40 0 0 0 0
  9/1/2019 - 9/7/2019 CP Hours-001381 40 0 20 20 0 0 0 0 0
  9/8/2019 - 9/14/2019 CP Hours-001537 40 10 10 20 0 0 0 0 0
  9/15/2019 - 9/21/2019 CP Hours-001604 40 0 40 0 0 0 0 0 0
  9/22/2019 - 9/28/2019 CP Hours-001795 40 0 5 35 0 0 0 0 0
  9/29/2019 - 10/5/2019 CP Hours-001956 40 0 0 20 5 5 5 5 0
  10/6/2019 - 10/12/2019 CP Hours-002020 40 0   35 0 5 0 0 0
  10/13/2019 - 10/19/2019 CP Hours-002182 40 0 20 10 10 0 0
0 0
1040 90 335 225 250 60 55 25 0
1040


-- 
Person Name
Human Job
Cell: 867-5309
Email:  name@bop.org
LinkedIn:  https://www.linkedin.com/in/CarbonLife/
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 a 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.编辑:我意识到这实际上并没有捕获最后一行数据,但我不想依赖固定的行标记宽度,因此我将其作为练习留给读者来确定何时想要最后一行结束。

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

相关问题 如何使用javascript将CSV数据转换为配对数组数据? - How to convert CSV data to paring array data using javascript? 使用 Javascript 将 CSV 数据转换为 JSON 格式 - Convert CSV data into JSON format using Javascript 如何使用 JavaScript 将 JSON 转换为 CSV - How to convert JSON to CSV using JavaScript 如何使用 javascript 获取屏幕上当前存在的表格数据并将其转换为 csv 文件 - How to fetch table data which is currently present on screen using javascript and convert it into csv file 如何通过使用 NodeJS 和 Javascript 使用具有子文档的 Mongoose 导入大型 CSV 文件(15M 数据)? - How to import large CSV file (15M Data) with Mongoose that has sub documents through using NodeJS and Javascript? 如何使用 Nodejs 将文件夹中的数据作为文件名写入 CSV 文件 - How to write a data as file names in a folder to CSV file using Nodejs 如何使用 angular 和 nodejs 将 csv 数据转储到 mongoDB - how to dump csv data into mongoDB using angular and nodejs 如何将csv数据保存到nodejs中的.csv文件中? - How to save csv data into .csv file in nodejs? 如何使用nodejs在服务器端将JavaScript动画转换为视频? - How to convert a javascript animation to video on the server-side using nodejs? 使用 javascript 将 JSON 转换为 CSV - Convert JSON to CSV using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM