Import CSV file (contains some non-UTF8 charac

2019-04-12 13:22发布

问题:

How can I import a CSV file that contains some non-UTF8 characters to MongoDB? I tried a recommended importing code.

mongoimport --db dbname --collection colname --type csv --headerline --file D:/fastfood.xls

Error Message

exception: Invalid UTF8  character detected

I would remove those invalid characters manually, but the size of the data is considerably big.

Tried Google with no success.

PS: mongo -v = 2.4.6

Thanks.

Edit: BTW, I'm on Win7

回答1:

In Linux you could use the iconv command as suggested in: How to remove non UTF-8 characters from text file

iconv -f utf8 -t utf8 -c file.txt

I'm not familiar with MongoDB, so I have no insight on how to preserve the invalid characters during import.



回答2:

For emacs users: Open CSV file in emacs and change encoding using ‘C-x C-m f’ and choosing utf-8 as the coding system. For more information see ChangingEncodings



回答3:

You're trying to import an xls file as a csv file. Save the file as csv first, then try again.