我有一個.txt文件,其中包含一列unixtime,比如(1322485992.420381000),點之前和之後的數字位數對於所有數據都是相同的。首先,我想將這個列導入到Mysql表中,我應該如何定義數據類型?數據類型轉換爲Mysql或python
CREATE TABLE videoinfo (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, unixtime char(50))
之後,在Python,我需要這個列轉換爲類似的日期時間:2011-11-28 14時25分23秒 我用results
保持從數據庫中讀取數據,然後嘗試將其轉換進入日期時間。 但它不能工作,它說datetime.fromtimestamp requires a float
。但是,如果我使用unixtime float(10,10)
創建列,則txt文件中的數據不能寫入數據庫。
results = cur.fetchall()
for result in results:
times = datetime.fromtimestamp(result[0])
cur.execute("ALTER TABLE youtube ADD date DATETIME NOT NULL DEFAULT after unixtime")
for time in times:
cur.execute(u'''insert into `date` values (%s)''', time)
任何人都可以幫忙嗎?非常感謝!!!
CNC中 for row in cur.fetchall(): print (row) times = datetime.fromtimestamp(float(row))
打印結果('1322485970.084063000',)
然後,該錯誤信息是TypeError: float() argument must be a string or a number.
那麼,如何可以獲取純粹的字符串值,以擺脫('',)
CNC中 使用行[0]相反...問題解決了...
好的,但我有一列數據需要轉換,而不僅僅是一個 – manxing
我用float來定義列,但就像我說的,問題是txt文件中的數據不能寫入數據庫,但是當它被定義爲char時,它可以寫成 – manxing
其實我的意思是在Python中使用'float()':'times = datetime.fromtimestamp(float(result [0]))' –