我在将 csv 数据加载到 mysql 表时遇到问题。
我有这样的数据:
Identity : name=Tomas, age=28, hoby=traveling;
Identity : name=Justin, age=29, hoby=Fhising;
我想要这样的结果
|field1|field2| field3 |
|tomas | 28 |traveling|
|justin| 29 | fhising |
LOAD DATA INFILE 'FILENAME.CSV'
INTO TABLE TABLENAME
(@f1, @f2, @f3)
FIELDS TERMINATED BY ','
LINES TERMINATED BY ';'
SET `field1` = SUBSTRING(@f1, LOCATE('name=', @f1)+5), `field2` = SUBSTRING(@f2, LOCATE('age=', @f2)+4), `field3` = SUBSTRING(@f3, LOCATE('hoby=', @f3)+5)
注意我没有测试过。所以可能会有错误。请根据您的需要进行调整。
awk
预处理器预处理您的 csv 文件awk
并调用LOAD DATA INFILE
awk -F[,=\;] '{print $2","$4","$6}' < FILENAME.CSV > PROPER.CSV
现在在mysql中,
LOAD DATA INFILE 'PROPER.CSV'
INTO TABLE TABLENAME
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'