我有一个csv文件,看起来像:
Dates Experiment Experiment_Type RESET_FREQUENCY MEASURE_LENGTH Value Date_Integer
2016-09-28 A FORWARD_Detector 1 Minute 1 0.99994 20160928
A FORWARD_Detector 1 Minute 7 0.99959 20160928
2016-09-28 A FORWARD_Detector 1 Minute 14 0.99917 20160928
2016-09-28 A FORWARD_Detector 1 Minute 21 0.99876 20160928
A FORWARD_Detector 1 Minute 30 0.99823 20160928
我收到的内容如下:
Dates Experiment Experiment_Type RESET_FREQUENCY MEASURE_LENGTH Value Date_Integer
2016-09-28 A FORWARD_Detector 1 Minute 1 0.99994 20160928
0000-00-00 A FORWARD_Detector 1 Minute 7 0.99959 20160928
2016-09-28 A FORWARD_Detector 1 Minute 14 0.99917 20160928
2016-09-28 A FORWARD_Detector 1 Minute 21 0.99876 20160928
0000-00-00 A FORWARD_Detector 1 Minute 30 0.99823 20160928
但我想要的是:
Dates Experiment Experiment_Type RESET_FREQUENCY MEASURE_LENGTH Value Date_Integer
2016-09-28 A FORWARD_Detector 1 Minute 1 0.99994 20160928
NULL A FORWARD_Detector 1 Minute 7 0.99959 20160928
2016-09-28 A FORWARD_Detector 1 Minute 14 0.99917 20160928
2016-09-28 A FORWARD_Detector 1 Minute 21 0.99876 20160928
NULL A FORWARD_Detector 1 Minute 30 0.99823 20160928
我的查询如下所示:
exp_data = """LOAD DATA LOCAL INFILE 'C:/Data.csv' IGNORE
INTO TABLE interest_rate_curves_bootstrap
FIELDS TERMINATED BY ';'
LINES TERMINATED BY "\\n"
(@var1,Experiment,Experiment_Type,RESET_FREQUENCY,MEASURE_LENGTH Value, Date_Integer)
set Dates = STR_TO_DATE(@var1, '%d-%b-%y');"""
我尝试了以下但没有奏效:
ALTER TABLE experiment_US MODIFY COLUMN experiment_data DATETIME NULL;
然后更新值:
UPDATE experiment_USSET experiment_data= NULL WHERE experiment_data= '0000-00-00';
import sqlalchemy as sqlal
#connection to mysql database
mysql_engine = sqlal.create_engine('mysql+mysqlconnector://xxx@localhost/rates data')
mysql_engine.raw_connection()
metadata = sqlal.MetaData()
experiment_data = sqlal.Table('experiment_data', metadata,
sqlal.Column('Date_String', sqlal.Date(), nullable=True),
sqlal.Column('Experiment', sqlal.String(3), nullable=True),
sqlal.Column('Experiment_Type', sqlal.String(8), nullable=True),
sqlal.Column('RESET_FREQUENCY', sqlal.String(3), nullable=True),
sqlal.Column('MEASURE_LENGHT', sqlal.Integer(), nullable=True),
sqlal.Column('Value', sqlal.Float(), nullable=True),
sqlal.Column('Date_Integer', sqlal.Integer(), nullable=True)
)
exp_data = """LOAD DATA LOCAL INFILE 'C:/Data.csv' IGNORE INTO TABLE experiment_data FIELDS TERMINATED BY ';' LINES TERMINATED BY "\\n" (@var1,Experiment,Experiment_Type,RESET_FREQUENCY,MEASURE_LENGTH Value, Date_Integer) set Dates = STR_TO_DATE(@var1, '%d-%b-%y');"""
mysql_engine.execute(exp_data)
答案 0 :(得分:0)
将最后一行更改为:
SET Dates = IF(@var1 = '', NULL, STR_TO_DATE(@var1, '%d-%b-%y'));"""
检查CSV字段是否为空,然后将NULL
放入表中,而不是调用STR_TO_DATE
。