2

我正在编写一个 python 脚本来获取格式化为StartTime="mm/dd/yyyy hh:mm:ss:ccc"EndTime="mm/dd/yyyy hh:mm:ss:ccc"位于文本文件中的字符串。

所以我继续搜索StartTimeEndtime但我怎样才能得到 ( ="mm/dd/yyyy hh:mm:ss:ccc") 之后的内容?当我得到StartTimeandEndTime字符串时,我想将它们保存在另一个带有函数的文本文件中,saveIntoFile(File, textToSave)但我想我可以自己处理这部分。

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):
4

1 回答 1

1
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
于 2013-04-23T14:43:27.403 回答