从文本文件中检索随机预格式化的文本 [英] Retrieve randomly preformatted text from Text File
问题描述
我正在编写一个python脚本,以获取位于文本文件中的格式为StartTime="mm/dd/yyyy hh:mm:ss:ccc"
和EndTime="mm/dd/yyyy hh:mm:ss:ccc"
的字符串.
I am writing a python script to get a string formatted as StartTime="mm/dd/yyyy hh:mm:ss:ccc"
and EndTime="mm/dd/yyyy hh:mm:ss:ccc"
located in a text file.
所以我继续搜索StartTime
和Endtime
,但是如何获取(="mm/dd/yyyy hh:mm:ss:ccc"
)之后的内容?当我得到StartTime
和EndTime
字符串时,我想将它们保存在具有功能saveIntoFile(File, textToSave)
的另一个文本文件中,但我想我自己就可以处理这一部分.
So I proceed to search for StartTime
and Endtime
but how can I get what's after (="mm/dd/yyyy hh:mm:ss:ccc"
) ? When I get the StartTime
and EndTime
strings I want to save them in another text file with a function saveIntoFile(File, textToSave)
but I think I'll be able to deal with this part myself.
def getTimeCode(File) :
fopen = open(File, 'r')
text = fopen.read()
for t in text :
if t == "StartTime" :
#what should I do now ?
if t == "EndTime" :
#what should I do now ?
def saveIntoFile(filename, textToSave):
推荐答案
import re
def getTimeCode(fn):
with open(fn, 'r') as f:
for line in f:
m = re.search(r'(\w)="(\d\d/\d\d/\d\d\d\d \d\d:\d\d:\d\d:\d\d\d)"', line)
if m:
if m.group(1) == 'StartTime':
# do something with m.group(2)
elif m.group(1) == 'EndTime':
# do something with m.group(2)
else:
# m.group(1) unknown
这篇关于从文本文件中检索随机预格式化的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!