简体   繁体   English

删除行首的“,” CSV

[英]Removing "," at the beginning of a line CSV

i want to convert a.csv file and write a new one.我想转换 a.csv 文件并编写一个新文件。 However I am not able to remove the first, i am kinda stuck here and it is driving me crazy.但是我无法删除第一个,我有点卡在这里,这让我发疯。 This is my code:这是我的代码:

    var extractedtasks = tasks.slice(0, 3)
    var extractedtasksformated = extractedtasks.toString().replace(/,$/g, "\n")

    let csvformat = "EMAIL,PASSWORD,MAILBOX"
    fs.writeFileSync(tasklocation[0], csvformat + "\n" + extractedtasksformated.replace(/,^/g, 
    ""))
    console.log(chalk.green("Successfully updated the CSV file"))

That's the output i am getting in the newly generated file那就是我在新生成的文件中得到的 output

EMAIL,PASSWORD,MAILBOX
example1@gmail.com,Password123,example@gmail.com:password
,example2@gmail.com,Password123,example@gmail.com:password
,example3@gmail.com,Password123,example@gmail.com:password

Output extractedtasks: Output 提取的任务:

[
  'example1@gmail.com,Password123,example@gmail.com:password\r',
  'example2@gmail.com,Password123,example@gmail.com:password\r',
  'example3@gmail.com,Password123,example@gmail.com:password\r'
]

Output extractedtasksformated: Output 提取的任务格式为:

,example3@gmail.com,Password123,example@gmail.com:passwordxample@gmail.com:password

Because extractedtasks is an array, instead of converting it to a string you should just join it with the expected separator:因为extractedtasks是一个数组,而不是将其转换为字符串,您应该将其与预期的分隔符连接起来:

extractedtasks = [
  'example1@gmail.com,Password123,example@gmail.com:password\r',
  'example2@gmail.com,Password123,example@gmail.com:password\r',
  'example3@gmail.com,Password123,example@gmail.com:password\r'
]

extractedtasksJoined = extractedtasks.join("\n")
// "example1@gmail.com,Password123,example@gmail.com:password\r\nexample2@gmail.com..."

// depending on the target line separator, you should also probably
// remove the "\r"
extractedtasksJoined = extractedtasksJoined.replace("\r", "")

// finally
fs.writeFileSync(tasklocation[0], csvformat + "\n" + extractedtasksJoined + "\n")

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

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