您的位置:首页 > 编程语言 > Python开发

导出数据库数据,转换时间格式----mysqldump SCP python

2012-05-18 04:38 731 查看
一开始使用Python就爱上了她!

1, Firstly ,I get the unixtime (start_timekey and end_timekey)use a

python file named timekey.py

timekey.py code:

#!/usr/bin/env python

import time

import datetime

#print time.ctime(1329053580)

#print time.ctime()

s = '(2012-05-14 00:00:00)'

s = s.lstrip('(').rstrip(')')

d = datetime.datetime.strptime(s,"%Y-%m-%d %H:%M:%S")

print time.mktime(d.timetuple())

s = '(2012-05-14 23:59:00)'

s = s.lstrip('(').rstrip(')')

d = datetime.datetime.strptime(s,"%Y-%m-%d %H:%M:%S")

print time.mktime(d.timetuple())

2, Secondly, I export the data what I want :

mysqldump -uroot -p meteo

sdata --where="timekey > start_timekey and timekey < end_timekey and

sensorid =11 and fieldid = 0 " > Temp20120514.txt

注: mysql> select * from mfield; 可以查看气象数据对应的fieldid

3,scp username@IP:/home/cvo/Temp20120514.txt /home/yuxin 传输文件

scp -r username@IP:/home/cvo/xyx/bootes4a_etcrts2/ /home/xyx 传输文件夹

4,delete the content except the raw data in the TXT file.

5, use the python named file.py to convert unixtime to normal date-time

format.

file.py code:

#!/usr/bin/env python

#encoding=utf-8

import sys

import os

import time

import datetime

def unixtime(s):

return time.ctime(s)

f = open(sys.argv[1])

fw = open(sys.argv[2],'w')

fw.writelines(" Date Temperature\n")

x = f.readline()

a=x.split(',')

for j in range(len(a)/4):

a[0+j*4]=a[0+j*4][1:]

a[0+j*4]=unixtime(int(a[0+j*4]))

a[3+j*4]=a[3+j*4][:-1]

line = a[0+j*4]+' '+a[3+j*4]+'\n'

print line

fw.writelines(line)

f.close()

fw.close()

原始导出数据:

(1336924860,11,0,10.2871),(1336924920,11,0,10.2778),(1336924980,11,0,10.2778),(1336925040,11,0,10.2778),(1336925100,11,0,10.2778),(1336925160,11,0,10.2778),(1336925220,11,0,10.2778),(1336925280,11,0,10.2778),(1336925340,11,0,10.2963),(1336925400,11,0,10.2778),(1336925460,11,0,10.3241),(1336925520,11,0,10.3147),(1336925580,11,0,10.3185),(1336925640,11,0,10.3092),(1336925700,11,0,10.2833),(1336925760,11,0,10.2778),(1336925820,11,0,10.2778),(1336925880,11,0,10.2778),(1336925940,11,0,10.2704),(1336926000,11,0,10.2407),(1336926060,11,0,10.2222),(1336926120,11,0,10.2222),(1336926180,11,0,10.2129),(1336926240,11,0,10.2037),(1336926300,11,0,10.1667),(1336926360,11,0,10.1667),(1336926420,11,0,10.1667),(1336926480,11,0,10.1538),(1336926540,11,0,10.1111),(1336926600,11,0,10.1111),(1336926660,11,0,10.063),(1336926720,11,0,10.0556),(1336926780,11,0,10.0556),(1336926840,11,0,10.0222),(1336926900,11,0,10),(1336926960,11,0,9.97964),(1336927020,11,0,9.94444),(1336927080,11,0,9.94444),(1336927140,11,0,9.94444),(1336927200,11,0,9.90926),(1336927260,11,0,9.88889),(1336927320,11,0,9.88889),(1336927380,11,0,9.87964),(1336927440,11,0,9.84074),(1336927500,11,0,9.83333),(1336927560,11,0,9.83333),(1336927620,11,0,9.8223),(1336927680,11,0,9.79081)

处理后数据:

Date/time Temperature

Mon May 14 00:01:00 2012 10.2871

Mon May 14 00:02:00 2012 10.2778

Mon May 14 00:03:00 2012 10.2778

Mon May 14 00:04:00 2012 10.2778

Mon May 14 00:05:00 2012 10.2778

Mon May 14 00:06:00 2012 10.2778

Mon May 14 00:07:00 2012 10.2778

Mon May 14 00:08:00 2012 10.2778

Mon May 14 00:09:00 2012 10.2963

Mon May 14 00:10:00 2012 10.2778

Mon May 14 00:11:00 2012 10.3241

Mon May 14 00:12:00 2012 10.3147

Mon May 14 00:13:00 2012 10.3185

Mon May 14 00:14:00 2012 10.3092

Mon May 14 00:15:00 2012 10.2833

Mon May 14 00:16:00 2012 10.2778

Mon May 14 00:17:00 2012 10.2778

Mon May 14 00:18:00 2012 10.2778

Mon May 14 00:19:00 2012 10.2704

Mon May 14 00:20:00 2012 10.2407

Mon May 14 00:21:00 2012 10.2222

Mon May 14 00:22:00 2012 10.2222

Mon May 14 00:23:00 2012 10.2129

Mon May 14 00:24:00 2012 10.2037
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐