Python:如何实现获取最新位置

标签 python

目标:- 我需要用下面的输入解析一个文件,并在状态被批准并且构建日期是最新的时获取最新的(构建日期时间戳应该是最新的)位置。我提供了示例输入和输出,我有 perl 中的代码,新的python,请建议如何在python中实现这一点

INPUT:-
Build:          M1234BAAAANAAW9321.1
Location:       \\dreyers\builds468\INTEGRATION\M1234BAAAANAA9321.1
Comments:       Build completed, labeled, and marked for retention.
Status:         Approved
BuildDate:      10/25/2012 12:51:25


Build:          M1234BAAAANAAW9321.2
Location:       \\crmbld01\Builds\FAILED\M1234BAAAANAA9321.2
Comments:       The build is currently in a failed status.
Status:         Failed
BuildDate:      10/25/2012 19:37:17


Build:          M1234BAAAANAAW9321.3
Location:       \\freeze\builds427\INTEGRATION\M1234BAAAANAA9321.3
Comments:       Build completed, labeled, and marked for retention.
Status:         Approved
BuildDate:      10/25/2012 19:43:28

OUTPUT:-\\freeze\builds427\INTEGRATION\M1234BAAAANAA9321.3

以下是实现的perl代码,我是python新手,请指教如何python化这段代码

$ echo 'Build:          M1234BAAAANAAW9321.1
Location:       \\dreyers\builds468\INTEGRATION\M1234BAAAANAAW9321.1
Comments:       Build completed, labeled, and marked for retention.
Status:         Approved
BuildDate:      10/25/2012 12:51:25


Build:          M1234BAAAANAAW9321.2
Location:       \\crmbld01\Builds\FAILED\M1234BAAAANAAW9321.2
Comments:       The build is currently in a failed status.
Status:         Failed
BuildDate:      10/25/2012 19:37:17


Build:          M1234BAAAANAAW9321.3
Location:       \\freeze\builds427\INTEGRATION\M1234BAAAANAAW9321.3
Comments:       Build completed, labeled, and marked for retention.
Status:         Approved
BuildDate:      10/25/2012 19:43:28
' | perl -e'

local $/ = "";
my ( $build_date, $location );
while ( <> ) {
next unless /status:\s+approved/i;
my $date = sprintf "%04d%02d%02d%02d%02d%02d", ( /builddate:\s+(\d
++)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)/i )[ 2, 0, 1, 3, 4, 5 ];
if ( !defined $build_date || $build_date lt $date ) {
    ( $build_date, $location ) = ( $date, /location:\s+(.+)/i );
    }                                                           
}    

打印“$位置\n”; ' \freeze\builds427\集成\M1234BAAAANAAW9321.3

我尝试用 python 编写以下内容,似乎没有用,我想知道是否有更好的实现来实现目标..请建议

for line_info in lines:

    line_info.find('Location')

    if line_info.find('Location') == 0:
        # Build Location
        print "  Found Build location"
        logString += "  Found Build location\n"
        location = line_info.split(" ")
        location1 = location[len(location)-1]
    elif line_info.find('Status') == 0:
        # Status
        print "  Found Status"
        logString += "  Found Status\n"
        status = line_info.split(" ")
        removeEmpty(status)
        status1 = status[1].strip()
        if status1 != "Approved"
        goto .start
    elif line_info.find('BuildDate') == 0:
        # Main Make
        print "  Found BuildDate"
        logString += "  Found BuildDate\n"
        builddate1 = line_info.split(" ")
        removeEmpty(builddate1)
        builddate1 = builddate1[1]
        #if builddate1 > 

最佳答案

试试这段代码:

lastLocation = None
lastTime = None
skip = False

bestLocation = None
bestTime = None

for line in text.split('\n'):    
    line.find('Location')
    if line.find('Location') == 0:
        # Build Location
        skip = False
        print "  Found Build location"
        lastLocation = line.split(":")[1].lstrip()       
    elif line.find('Status') == 0:
        # Status
        print "  Found Status"
        status = line.split(":")
        status1 = status[1].strip()
        if status1 != "Approved":
            skip = True
    elif line.find('BuildDate') == 0 and not skip:
        # Main Make
        print "  Found BuildDate"
        timeStr = line.split(":", 1)[1].lstrip()
        lastTime = datetime.datetime.strptime(timeStr, "%m/%d/%Y %H:%M:%S")
        if bestTime == None or bestTime < lastTime:
            bestTime = lastTime
            bestLocation = lastLocation
print lastLocation

关于Python:如何实现获取最新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13206579/

相关文章:

python - 验证架构并将其应用于 python 中的列表

python - QMediaContent 如何在 urllib 无法下载时显示视频?

python - 如何在 Selenium 中找到这些复选框元素

python - 在pyqt中使用sceneEventFilter捕获悬停事件

Python半菜鸟,谁能解释一下为什么这个现象出现在 'Lists'

python - Apache Pig - 如何维护分布式查找表供我的 python UDF 访问?

python - 如何在 Python 中对数据帧的字符串进行哈希处理?

python - 线程和 Django 数据库创建操作

python - 如何在不使用任何库的情况下使用 Python 实现一个简单的 Web 服务器?

Python 将 k-means 集群与实例相关联