0

I'm using the csv module along with csv-parse, stream-transform and csv-stringify modules to read in an excel file and select the columns I need. The CSV file contains special characters in Portuguese that cannot be encoded properly because I ignore the BOM character so that I can get the correct column names. I wanted to know if there was a way to add the BOM character to the resulting file so that the file can be properly encoded? Here are the options I use for the parser:

parse({
    columns:true,
    delimeter: ',', 
    bom: true
})
user8565662
  • 127
  • 1
  • 1
  • 6
  • Are you saying you want a transform stream that adds a BOM at the start? Or are you saying the first row has a different endianness than the rest? – David Knipe Aug 30 '19 at 21:13
  • @DavidKnipe the former. – user8565662 Aug 30 '19 at 22:35
  • Problem solved. If anyone else is having the same issue you can do `fs.writeFile(filename, '\ufeff', callback());` to write the bom character and then when you create your writeStream `fs.createWriteStream(filename, {flag: 'a'});` Then you can use `pipe()` as normal. – user8565662 Aug 31 '19 at 07:06
  • You're calling `callback` synchronously, before you ever write to the stream. You meant `callback`, not `callback()`. If you call the callback function before calling `fs.writeFile` then there's no guarantee that the BOM will be at the start. Or as the documentation says: "It is unsafe to use `fs.writeFile()` multiple times on the same file without waiting for the callback." https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback – David Knipe Sep 01 '19 at 10:57

0 Answers0