当某些字段未用双引号引起来且包含新行时,将 CSV 文件加载到 MySQL 表中
我有一个 CSV 文件,我想使用以下命令将其加载到 MySQL 表中:
LOAD DATA LOCAL INFILE '/path/to/file.csv'
INTO TABLE items
CHARACTER SET utf8
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(field1, field2, field3, field4, field5);
我面临的问题是 csv 文件格式不正确,因为某些字段没有用双引号 ("") 括起来,并且还有新行。例如:(第三行)
"field1","field2","field3","field4","field5"
"aaaaa","bbbbb","ccccc","ddddd","eeeeee"
aaaa
aaaa,bbbbbbbb
bbbbb,"ccccc","dddddd","eeeee"
当我将 csv 文件导入 MySQL 时,字段内容内的换行符被解释为行终止。
那么……我该如何解决呢?正则表达式?一些 CSV 编辑器(我尝试过 CSVed 但没有成功)?谢谢。
I have a CSV file that I want to load into a MySQL table using the following command:
LOAD DATA LOCAL INFILE '/path/to/file.csv'
INTO TABLE items
CHARACTER SET utf8
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(field1, field2, field3, field4, field5);
The problem I face is that the csv file is not properly formatted because some fields are not enclosed by double quotes ("") and have also new lines. e.g.: (third line)
"field1","field2","field3","field4","field5"
"aaaaa","bbbbb","ccccc","ddddd","eeeeee"
aaaa
aaaa,bbbbbbbb
bbbbb,"ccccc","dddddd","eeeee"
When I import the csv file into MySQL, newlines inside field contents are interpreted as a line termination.
So... how can I sort it out? Regex? Some CSV editor (I tried CSVed with no luck)? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速而肮脏的修复尝试:
应用于您的数据片段输出:
Quick and dirty fix attemt:
Applied on your data snippet outputs: