mongoimport:csv文件在引用字段错误内结束

时间:2014-07-18 05:14:28

标签: mongodb csv

将csv文件导入mongo数据库时,出现错误: exception : CSV file ends while inside quoted field并且导入操作中止。我检查了它在异常描述中引用的实际记录。我能够将该记录独立导入mongodb。当我尝试导入整个文件时,它不起作用。

这个问题可能是什么原因?我手动检查了该记录,并且还能够将该记录独立导入mongodb。引号/逗号或其他可能导致问题的特殊字符仍然存在问题吗?

根据例外情况:错误在第3行 这行前后还有一些行。我正在使用带参数的mongoimport

mongoimport -h 127.0.0.1 -d databasename -c collectionname --type csv --file filelocation --headerline --ignoreBlanks
connected to: 127.0.0.1
exception:CSV file ends while inside quoted field
2014-07-17T22:39:29.629-0700 check 9 6307
2014-07-17T22:39:29.631-0700 imported 6306 objects
encountered 1 error(s)

第1行:

"1555502","149","2012-07-26 17:13:10","iantist","2014-05-02 17:39:48","http://twitter.com/@iantist",,N,"<p>Neuroscientist,, rabid R user at work and home. </p>&#xA;","21","8","0",,N,,N,,N,"1442157"

第2行:

"1555662","794","2012-07-26 18:18:37","olivecoder","2014-05-02 16:09:38","http://www.linkedin.com/in/olivecoder","Brazil","<p>I am a software engineer focused on developing solutions for the telecommunications industry since 1990. I have a huge background in R&amp;D projects,, developing firmware for telecom equipment,, CTI applications,, softswitches,, IVR and others telecom solutions that resulted in patent registration. I founded Mares Telecom to provide value-added services to carriers and to create new technologic solutions for the telecom market.</p>&#xA;&#xA;<p><a href=,"http://linkedin.com/in/olivecoder," rel=,"nofollow,">http://linkedin.com/in/olivecoder</a></p>&#xA;","135","75","8",,N,,N,"42","1693823"

第3行:

"1556310","15","2012-07-27 00:07:09","Volodymyr Zhabiuk","2013-05-14 01:13:28","http://www.linkedin.com/in/volodymyrzhabiuk",,N,,N,"9","0","0",,N,,N,,N,"1694670"

第4行:

"1557339","35","2012-07-27 10:09:27","Brice","2014-05-03 06:00:33","http://fr.linkedin.com/in/bleporini",,N,,N,"7","1","0",,N,,N,,N,"1695972"

第5行:

"1558307","253","2012-07-27 17:06:29","PurpleDiane","2014-04-03 22:39:43","http://www.linkedin.com/in/dianedemerschen","San Diego,, CA","<p>20+ years programming experience,, primarily in Computer-Aided Design and Manufacturing. Early Fortran,, migrated to C. Took time off to care for my mom when she had Alzheimer's. Now updating my skills in C++,, Java,, Python and Android programming. At least that's what I've studied so far!</p>&#xA;&#xA;<p>I got a lot of information from StackOverflow while I was taking my C++ classes,, now I hope to learn more and perhaps even be of some help.</p>&#xA;","25","6","1",,N,,N,,N,"1697171"

1 个答案:

答案 0 :(得分:0)

感谢您的帮助!!事实证明,问题在于整个文件中各行中具有空值的字段!解释了类似的问题here

我实际上是从Mysql数据创建了CSV文件。所以我使用这个命令导出到csv(但没有任何转义/封闭条件)

select 'column1','column2'
UNION ALL
SELECT *
    FROM Mysqltable
    into outfile 'Filename.csv';

这被导出为csv文件(来自Mysql数据库)但列条目之间没有逗号或用双引号括起来。

导入mongo时:使用与上面相同的命令,但导入为TSV(制表符分隔值)。

mongoimport -h 127.0.0.1 -d databasename -c collectionname --type tsv --file filelocation --headerline --ignoreBlanks 

现在完美运作!