PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] How can I read ch-zh, ru, ja character in csv?

 August 29, 2022     csv, excel, javascript     No comments   

Issue

I draft a code to generate i18n.csv

const fs = require("fs")

async function main() {
  let table = [ 
    [ "en", "ch-zh", 'ru', 'ja' ],
    [ "Hello", "你好", "Привет", "こんにちは" ]
  ]

  fs.writeFileSync(__dirname +"/i18n.csv", array2csv(table), { encoding: "utf8", flag: 'a', mode: 0o666 })
}
main()

function array2csv(arr){
  arr = arr.map((col) => { return col.join(",") })
  arr = arr.join("\n")
  return arr
}

And I got the result as my expected in the file:

en,ch-zh,ru,ja
Hello,你好,Привет,こんにちは

But I cannot see the data if the characters are not a english character in excel, how can I fix it?

enter image description here


Solution

Thanks all guys for the helps, it works now!

[Summary] Prepend a UTF-8 BOM in the csv file

const fs = require("fs")

async function main() {
  let table = [ 
    [ "en", "ch-zh", 'ru', 'ja', 'emoji' ],
    [ "Hello", "你好", "Привет", "こんにちは", '✅' ]
  ]

  fs.writeFileSync(__dirname +"/i18n.csv", array2csv(table), { encoding: "utf8", flag: 'a', mode: 0o666 })
}
main()

function array2csv(arr){
  arr = arr.map((col) => { return "\ufeff" + col.join(",") })
  arr = arr.join("\n")
  return arr
}

enter image description here



Answered By - Yuu
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing