Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

node.js - Mysql export CSV then parse in nodejs with csv-parse does not find separator properly

I am exporting a description field from a mysql db:

SELECT REPLACE(description, char(13), '\n') 
FROM table u  INTO OUTFILE '/var/lib/mysql-files/u.csv' 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '
';

Then in nodejs I process the file using csv-parse (must use this one as it handles async):

const parse       = promisify(require('csv-parse'));
const records = await parse(fileContents, { delimiter: ',', columns: true, skip_empty_lines: true, record_delimiter: "
" });

Here is an example text:

Married, mad scientist and tennisman from Germany. I might not be able
to spell the word "ubuntu" without spellcheck software, but be sure I
know the meaning.

The parser however get it all wrong, split it in 3 fields:

'"Married, mad scientist and tennisman from Germany. I might not be
able to spell the word \"ubuntu\" without spellcheck software', 
'but be sure I know the meaning."',  
'\N'

The 3rd field comes from mysql, for some reason the file is saved with this N at the end instead of . I can remove it by sed, as well as the end of line comma, though some tips always appreciated if my query could avoid that.

However for the split of the main text in 2 fields, I am puzzled. I tried removing the delimiter parameter in parse, but same issue. I tried relax: true, same.

Any idea what am I doing wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...