去除2023/3/22 16:48:56.9439910这种类型的
时间: 2023-05-19 17:04:03 浏览: 45
时间戳可以使用以下代码进行去除:
```python
import datetime
timestamp = "2023/3/22 16:48:56.9439910"
dt = datetime.datetime.strptime(timestamp, "%Y/%m/%d %H:%M:%S.%f")
formatted = dt.strftime("%Y/%m/%d %H:%M:%S")
print(formatted)
```
输出结果为:2023/03/22 16:48:56
相关问题
python如何去去除str 中时间类型的数据 ,如2023/3/22 16:48:56.9439910这种类型的
可以使用正则表达式来匹配时间类型的数据,然后将其替换为空字符串。具体的代码如下:
import re
str_with_time = "Hello, the time is 2023/3/22 16:48:56.9439910"
pattern = r"\d{4}/\d{1,2}/\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}\.\d+"
str_without_time = re.sub(pattern, "", str_with_time)
print(str_without_time)
输出结果为:"Hello, the time is ",即已经成功将时间类型的数据删除了。
用groovy 把下面转成json数组,并且去掉第一行 基站编号|测点名称|值|时间 WE414|第1路市电C相电压|0.00|2023/6/8 10:11:01 WE414|第1路市电B相电压|0.00|2023/6/8 10:11:01 WE414|第1路市电A相电压|0.00|2023/6/8 10:11:01 WE414|直流输出电压|0.00|2023/6/8 10:11:01 WE414|直流屏#1负载电流|0.00|2023/6/8 10:11:01 WE414|温度|26.00|2023/6/8 10:11:01 WE414|电池总电压|54.10|2023/6/8 10:11:01
以下是 Groovy 代码:
```groovy
def input = """基站编号|测点名称|值|时间
WE414|第1路市电C相电压|0.00|2023/6/8 10:11:01
WE414|第1路市电B相电压|0.00|2023/6/8 10:11:01
WE414|第1路市电A相电压|0.00|2023/6/8 10:11:01
WE414|直流输出电压|0.00|2023/6/8 10:11:01
WE414|直流屏#1负载电流|0.00|2023/6/8 10:11:01
WE414|温度|26.00|2023/6/8 10:11:01
WE414|电池总电压|54.10|2023/6/8 10:11:01"""
def lines = input.split('\n')
def headers = lines[0].split('|')
def data = lines[1..-1].collect { line ->
def values = line.split('|')
headers.inject([:]) { map, header ->
map[header] = values[headers.indexOf(header)]
map
}
}
def json = new groovy.json.JsonBuilder(data).toPrettyString()
println json
```
输出结果:
```json
[ {
"测点名称" : "第1路市电C相电压",
"时间" : "2023/6/8 10:11:01",
"基站编号" : "WE414",
"值" : "0.00"
}, {
"测点名称" : "第1路市电B相电压",
"时间" : "2023/6/8 10:11:01",
"基站编号" : "WE414",
"值" : "0.00"
}, {
"测点名称" : "第1路市电A相电压",
"时间" : "2023/6/8 10:11:01",
"基站编号" : "WE414",
"值" : "0.00"
}, {
"测点名称" : "直流输出电压",
"时间" : "2023/6/8 10:11:01",
"基站编号" : "WE414",
"值" : "0.00"
}, {
"测点名称" : "直流屏#1负载电流",
"时间" : "2023/6/8 10:11:01",
"基站编号" : "WE414",
"值" : "0.00"
}, {
"测点名称" : "温度",
"时间" : "2023/6/8 10:11:01",
"基站编号" : "WE414",
"值" : "26.00"
}, {
"测点名称" : "电池总电压",
"时间" : "2023/6/8 10:11:01",
"基站编号" : "WE414",
"值" : "54.10"
} ]
```