简体   繁体   English

Node.js-如何在csv中提取特定列

[英]Node.js - how to extract specific column in csv

I need to extract specific column in CSV file and create an output file as index.conf . 我需要提取CSV文件中的特定列并创建一个输出文件作为index.conf In this case I need to extract column under Service Manager and System Owner and have them as an output file as index.conf . 在这种情况下,我需要提取Service ManagerSystem Owner下的列,并将它们作为index.conf的输出文件。 Anyone can help? 有人可以帮忙吗?

Image of my CSV: 我的CSV图片: CSV

Client|*Description|Environment|LifeCycle|Service Manager|System Owner|
xxxxxx| xxxxxx     |Production |Active   |Mark Wright    |David harvey| 
xxxxxx| xxxxxx     |Production |Active   |John Stone     |  

I'm not a developer but just taking over some part of the job scope including maintaining a web site. 我不是开发人员,而是仅负责部分工作,包括维护网站。

You can use csv npm package for this. 您可以为此使用csv npm软件包。 It helps to generate, parse, transform and stringify CSV data. 它有助于生成,解析,转换和字符串化CSV数据。 It is a widely used npm package. 这是一个广泛使用的npm软件包。

  1. Use csvtojson library to convert csv to JSON format. 使用csvtojson库将cv转换为JSON格式。

    npm i --save csvtojson npm我-保存csvtojson

  2. Using JSON parsed from the above step, you can create .conf file as per your requirements. 使用从上述步骤解析的JSON,您可以根据需要创建.conf文件。

Refer to following code snippet for the implementation. 有关实现,请参考以下代码段。

In the following program, I'm considering only the first record of csv. 在以下程序中,我仅考虑csv的第一条记录。 You can write your own logic as per your requirements. 您可以根据需要编写自己的逻辑。

index.js index.js

// import csvtojson package
const csvtojson = require('csvtojson');
// import fs pacakge to write .conf file
const fs = require('fs')

/**
 * read csv file and save .conf file
 * @param {*} filePath path to the csv file
 */
async function readCSVAndSaveConf(filePath){
    // read csv file on the given path and get csv as json
    const parsedCSV = await readCSV(filePath);
    // save conf as per first record of csv. ** you can write your own logic
    saveConf(parsedCSV[0])
}

/**
 * read csv on given path
 * @param {*} filePath path to csv file
 */
function readCSV(filePath){
return csvtojson({ delimiter: '|' }).fromFile(filePath); 
}

/**
 * save conf file as per json
 * @param {*} jsonConfig 
 */
function saveConf(jsonConfig){
    // create .conf content
    const conf=`# This is conf file
    ServiceManager=${jsonConfig['Service Manager']}
    ServiceOwner=${jsonConfig['System Owner']}`
    // save .conf to the file
    fs.writeFileSync(__dirname+'/index.conf', conf);
}

// path to csv
const filePath="./input.csv"

// read csv and save conf
readCSVAndSaveConf(filePath)
.then(()=>console.log("completed"))
.catch(err=>console.error(err))

input.csv input.csv

Client|*Description|Environment|LifeCycle|Service Manager|System Owner|
xxxxxx| xxxxxx     |Production |Active   |Mark Wright    |David harvey| 
xxxxxx| xxxxxx     |Production |Active   |John Stone     |  

index.conf index.conf

ServiceManager=Mark Wright
ServiceOwner=David harvey

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM