简体   繁体   English

从文件中删除多余的字符串

[英]remove extra strings from file

Is there any wany to remove the date and heartrate: from the result and them save them in file有没有办法从结果中删除日期和心率:并将它们保存在文件中

2022.12.08 14:49:55 Heartrate: -1 2022.12.08 14:49:55 Resting Agitation Score: -1 2022.12.08 14:49:56 Heartrate: -1 2022.12.08 14:49:56 Resting Agitation Score: -1 2022.12.08 14:49:57 Heartrate: -1 2022.12.08 14:49:57 Resting Agitation Score: -1 2022.12.08 14:49:58 Heartrate: 32 2022.12.08 14:49:58 Resting Agitation Score: -1 2022.12.08 14:49:59 Heartrate: 31 2022.12.08 14:49:59 Resting Agitation Score: -1 2022.12.08 14:50:00 Heartrate: 32 2022.12.08 14:50:00 Resting Agitation Score: -1 2022.12.08 14:50:01 Heartrate: -1 2022.12.08 14:50:01 Resting Agitation Score: -1 2022.12.08 14:50:02 Heartrate: -1 2022.12.08 14:50:02 Resting Agitation Score: -1 2022.12.08 14:50:03 Heartrate: -1 2022.12.08 14:50:03 Resting Agitation Score: -1 2022.12.08 14:49:55 心率:-1 2022.12.08 14:49:55 静息激越得分:-1 2022.12.08 14:49:56 心率:-1 2022.12.08 14:49:56 静息激越得分: -1 2022.12.08 14:49:57 心率:-1 2022.12.08 14:49:57 静息激越分数:-1 2022.12.08 14:49:58 心率:32 2022.12.08 14:49:58 静息激越分数:-1 2022.12.08 14:49:59 心率:31 2022.12.08 14:49:59 静息激越分数:-1 2022.12.08 14:50:00 心率:32 2022.12.08 14:50:00 静息激越分数:-1 2022.12.08 14:50:01 心率:-1 2022.12.08 14:50:01 静息激动得分:-1 2022.12.08 14:50:02 心率:-1 2022.12.08 14:50:02 静息激动分数:-1 2022.12.08 14:50:03 心率:-1 2022.12.08 14:50:03 静息激动分数:-1

I want output as我想要 output 作为

-1 -1

-1 -1

-1 -1

-1 -1

-1 -1

Assuming your input is in a text file you could read the whole file as string into a variable and do something like this:假设您的输入是在一个文本文件中,您可以将整个文件作为字符串读入一个变量并执行如下操作:

 input_string = """2022.12.08 14:49:55 Heartrate : -1 2022.12.08 14:49:55 Resting Agitation Score : -1
     2022.12.08 14:49:56 Heartrate : -1 2022.12.08 14:49:56 Resting Agitation Score : -1
     2022.12.08 14:49:57 Heartrate : -1 2022.12.08 14:49:57 Resting Agitation Score : -1
     2022.12.08 14:49:58 Heartrate : 32 2022.12.08 14:49:58 Resting Agitation Score : -1
     2022.12.08 14:49:59 Heartrate : 31 2022.12.08 14:49:59 Resting Agitation Score : -1
     2022.12.08 14:50:00 Heartrate : 32 2022.12.08 14:50:00 Resting Agitation Score : -1
     2022.12.08 14:50:01 Heartrate : -1 2022.12.08 14:50:01 Resting Agitation Score : -1
     2022.12.08 14:50:02 Heartrate : -1 2022.12.08 14:50:02 Resting Agitation Score : -1
     2022.12.08 14:50:03 Heartrate : -1 2022.12.08 14:50:03 Resting Agitation Score : -1"""

lines = input_string.split('\n')
for line in lines:
    print(line.split(':')[-1].strip()) # prints items after last ':' without whitespace

if you have an input file and want an output file it could look something like this:如果你有一个输入文件并且想要一个 output 文件,它可能看起来像这样:

with open("input.txt", "r", encoding="utf-8") as file:
lines = file.readlines()

with open("output.txt", "w", encoding="utf-8") as file:
    for line in lines:
        file.write(line.split(':')[-1].strip() + '\n')

You may want to consider a regular expression for getting the score您可能需要考虑使用正则表达式来获取分数

import re

sensor_input = '2022.12.08 14:49:55 Heartrate : -1 2022.12.08 14:49:55 Resting Agitation Score : -1'

# Find and extract score
match_ = re.search('Resting Agitation Score : (-?\d*)', sensor_input)

int(match_.group(1)) # returns -1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM