小编Ger*_*ryC的帖子

使用 Python 读取带有日期对象和浮点数的逗号分隔文件

我有一个文件,其中的条目看起来像

2013-12-11 23:00:27.003293,$PAMWV,291,R,005.8,M,A*36
2013-12-11 23:00:28.000295,$PAMWV,284,R,005.5,M,A*3F
2013-12-11 23:00:29.000295,$PAMWV,273,R,004.0,M,A*33
2013-12-11 23:00:30.003310,$PAMWV,007,R,004.9,M,A*3B
Run Code Online (Sandbox Code Playgroud)

考虑到分隔符实际上是一个逗号 (','),这是一个经典的 CSV 文件。

我试过了:

wind = loadtxt("/disk2/Wind/ws425.log.test", dtype(str,float), delimiter=',', usecols=(0,4))
ts= time.strptime(str(wind[:,0]), '%Y-%m-%d %H:%M:%S.%f')
Run Code Online (Sandbox Code Playgroud)

我得到的是

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-484b71dea724> in <module>()
----> 1 ts= time.strptime(str(wind[:,0]), '%Y-%m-%d %H:%M:%S.%f')

/opt/Enthought/canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/_strptime.pyc in _strptime_time(data_string, format)
    452 
    453 def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
--> 454     return _strptime(data_string, format)[0]

/opt/Enthought/canopy/appdata/canopy-1.0.3.1262.rh5-x86_64/lib/python2.7/_strptime.pyc in _strptime(data_string, format)
    323     if not found:
    324         raise ValueError("time data %r does not match format %r" %
--> 325 …
Run Code Online (Sandbox Code Playgroud)

python csv datetime

2
推荐指数
1
解决办法
9235
查看次数

需要使用Python进行每日日志轮换(0utc)

我是Python的入选者.我写了一个小记录器,从串口获取数据并将其写入日志文件.我有一个小程序打开文件追加,写,然后关闭.我怀疑这可能不是最好的方法,但这是我到目前为止所想到的.

我希望能够在00 UTC自动执行日志轮换,但到目前为止,我尝试使用RotatingFileHandler执行此操作失败了.

这是代码的样子:

import time, serial, logging, logging.handlers,os,sys
from datetime import *

CT12 = serial.Serial()
CT12.port = "/dev/ct12k"
CT12.baudrate = 2400
CT12.parity = 'E'
CT12.bytesize = 7
CT12.stopbits = 1
CT12.timeout = 3

logStart = datetime.now()
dtg = datetime.strftime(logStart, '%Y-%m-%d %H:%M:%S ')
ctlA = unichr(1)
bom = unichr(2)
eom = unichr(3)
bel = unichr(7)
CT12Name = [ctlA, 'CT12-NWC-test']
CT12Header = ['-Ceilometer Logfile \r\n', '-File created: ', dtg, '\r\n']

def write_ceilo ( text ) :
    f = open ('/data/CT12.log', 'a') …
Run Code Online (Sandbox Code Playgroud)

python logging pyserial

1
推荐指数
1
解决办法
4389
查看次数

标签 统计

python ×2

csv ×1

datetime ×1

logging ×1

pyserial ×1