如何在Javascript中解析CSV [英] How to Approach Parsing CSV in Javascript
问题描述
(我的第一篇文章,如有任何错误,我深表歉意)
(My first post, I apologise for any mistakes)
我正在处理CSV文件中的一小部分数据,我需要阅读,处理,然后导出为文本文件。
I'm working with a small set of data in CSV files, which I need to read, process, and then export as a text file.
CSV数据的格式为:
The format of the CSV data is:
REGO,STATUS,SHIFT,LOCATION,LOADED
CCA4110,StatusON,5:43,Brisbane,1
CCA4112,StatusON,5:44,Syndey,0
CCA4118,StatusON,6:11,Melbourne,1
我希望能够标题行,并检查
I want to be able to take each line after the header row, and check
a)如果 LOADED值等于0或1(如果为1,则跳至下一行)。
a) if the 'LOADED' value equals 0 or 1 (skip to next row if 1).
b)如果 LOADED为0,则检查 REGO值是否与 REGO值的预定义列表匹配。
b) If 'LOADED' is 0, then check if the 'REGO' value matches a pre-defined list of 'REGO' values.
c)如果匹配,请更改 SHIFT时间。
c) If a match, change the 'SHIFT' time.
d)如果不匹配,请移至下一行。
d) If no match, move on to next row.
之后,我要导出所有内容行中只有'REGO'和'SHIFT'值的样子:
After that, I want to export all of the rows, with only the 'REGO' and 'SHIFT' values, to look like:
CCA4110,5:43
CCA4112,5:33
...
因为这有点复杂对我来说,我很难想象解决此问题的最佳方法。我想知道是否有人可以帮助我考虑一下这种方式,而不仅是一起破解一堆嵌套的循环。
Because this feels a little complex to me, I'm having trouble visualising the best way to approach this problem. I was wondering if someone could help me think about this in a way that isn't just hacking together a dozen nested for loops.
非常感谢,
利亚姆
编辑:有关检查多个条件的问题:
说我有两个CSV文件:
Say I have two CSV files:
List_to_Change.csv
REGO,STATUS,SHIFT,LOCATION,LOADED
CCA2420,StatusOn,11:24,BRISBANE,1
CCA2744,StatusOn,4:00,SYDNEY,1
CCA2009,StatusOn,4:00,MELBOURNE,0
List_to_Compare.csv
REGO,CORRECT_SHIFT
CCA2420,6:00
CCA2660,6:00
CCA2009,5:30
一种算法:
1。。检查 List_to_Check.csv LOADED列中的值
1. Check value in 'List_to_Check.csv' 'LOADED' column
A。如果值等于 0,请转到步骤2。
A. If value equals '0' go to step 2.
B。如果值等于 1,请跳过此行并转到下一个。
B. If value equals '1' skip this row and go to next.
2。。检查 List_to_Check.csv中的 REGO值是否显示在 List_to_Compare.csv中。
2. Check if 'REGO' value in 'List_to_Check.csv' shows up in 'List_to_Compare.csv'
A。如果是,则转到步骤3。
A. If true go to step 3.
B。如果为false,则跳过此行并转到下一步。
B. If false skip this row and go to next.
3。。将 List_to_Change.csv中的 SHIFT值更改为 List_to_Compare.csv'
3. Change 'SHIFT' value in 'List_to_Change.csv' with value shown in 'List_to_Compare.csv'
4。字符串化已更改的每一行并导出到文本文件。
4. Stringify each row that was changed and export to text file.
推荐答案
我的建议是将工作流程分为三个步骤:
My advice would be to split the work flow in to three steps:
- 将所有行解析为javascript对象
- 对对象数组执行逻辑
- 将对象字符串化为CSV
// This creates an object based on an order of columns:
const Entry = ([rego, status, shift, location, loaded]) =>
({ rego, status, shift, location, loaded });
// Which entries are we interested in?
const shouldHandleEntry = ({ loaded }) => loaded === "1";
// How do we update those entries?
const updateEntry = entry => ({
...entry,
shift: ["CCA4118"].includes(entry.rego)
? "5:33"
: entry.shift
});
// What do we export?
const exportEntry = ({ rego, shift }) => `${rego},${shift}`;
// Chain the steps to create a new table:
console.log(
csvBody(getCSV())
.map(Entry)
.filter(shouldHandleEntry)
.map(updateEntry)
.map(exportEntry)
.join("\n")
)
// (This needs work if you're using it for production code)
function csvBody(csvString) {
return csvString
.split("\n")
.map(line => line.trim().split(","))
.slice(1);
};
function getCSV() { return `REGO,STATUS,SHIFT,LOCATION,LOADED
CCA4110,StatusON,5:43,Brisbane,1
CCA4112,StatusON,5:44,Sydney,0
CCA4118,StatusON,6:11,Melbourne,1`; }
这篇关于如何在Javascript中解析CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!