导出数据库数据,转换时间格式----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
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
相关文章推荐
- 从数据库取到数据转换json后,时间格式带上T的解决办法
- 从数据库取到数据转换json后,时间格式带上T的解决办法
- Jsp中怎么把数据从数据库导出在页面直接生成html格式??
- 数据库基础(字符串函数、时间日期函数、数据类型转换、函数转换)
- java.sql.Data java.util.Data的转换 及时间格式化后的两者之间的转换,数据库中时间存储格式
- winform中将数据以文本的格式导出到excel中(以文本导出表示如果数据为0001则在excel中也显示为0001,而不被excel自动转换为1)
- php将从数据库中获得的数据转换成json格式并输出
- IOS开发时间格式转换之──C语言中time_t数据类型转为NSString类型
- 运用exp 导出oracle数据与 运用imp 将dmp格式数据导入数据库
- 数据库时间转换成字符串格式大全
- PowerDesigner从数据库导出pdm以及导出html格式数据字典
- joson返回数据库的时间格式在前台用js转换
- json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)
- layui数据表格时间戳转换为时间格式的方法
- CK+表情数据库转换为python数据格式保存(.pkl)
- 对数据库中时间为空的处理和数据转换成百分数
- 把数组格式数据转换成字符串存入数据库
- 如何导入excel数据到数据库,并解决导入时间格式问题
- c#将数据库数据转换为json格式(使用extjs,easyui中的datagrid)
- 具有强大构建强大的Word报表,支持XAML数据绑定,文档格式转换,导出为PDF等功能的控件TX Text Control .NET for WPF介绍