获取指定单词后的数据

时间:2017-02-08 12:28:15

标签: python regex python-2.7

    EVENT # :77

actualEndTime= 'None' allowCopyToPlanner= 'True' audioType= '2' audioTypeName= 'Simple stereo' baseType= '2' baseTypeName= 'EPG item booking' bookedAsOPPV= 'False' bookingActive= '1' bookingDeletedTime= 'None' bookingDiskQuotaName= 'user' bookingExpirationTime= '19700101T01:00:00' bookingKeep= 'True' bookingLock= 'False' bookingSource= '1' bookingTime= '20170208T11:00:00' bookingType= '2' bookingTypeName= 'Recording' boxSetLocator= 'None' boxSetName= 'None' boxSetNameSort= 'None' boxSetRating= 'None' canonicalName= 'COLD CASE' cgmsa= '0' channelID= 'xsi://FE2' cINFO::root:Trying main server 10.10.8.25:9000::224

DEBUG::root:Cookies: 'TWISTED_SESSION=3b6bc8c187388ab6bf57c7f887cf04a8'::107

DEBUG::root:Disconnected::398

hannelIDType= 'nds.com_URI' channelName= 'Sky Living HD' channelNr= '107' class= 'object.item.videoItem' cmdcMemberNumber= '0' description= 'Daniela: When they receive a home movie in which a young woman is apparently shot and killed, Rush and Valens reopen an unsolved murder case from 1979. (S2, ep 3)' episodeNumber= '0' episodeTitle= 'None' estimatedBitRate= '11534336' expireFromPlanner= 'False' extensionStartDur= '0' flagsAllowanalogtaping= 'True' flagsCopyprotected= 'False' flagsCurrentseries= 'True' flagsHasaudiodesc= 'False' flagsHasforeignsubtitles= 'True' flagsHd= 'True' flagsIppv= 'False' flagsIs3D= 'False' flagsIsadult= 'False' flagsIslinked= 'True' flagsOppv= 'False' flagsWidescreen= 'True' genre= '3' genreExtended= '3,772' genreId= 'nds.com_internal' genreName= 'Entertainment' groupID= '0' guardEndDur= '120000' guardStartDur= '120000' isBTO= 'False' isPdlTrailer= '0' isPlaying= 'False' isSeriesLinked= 'False' isShowLinked= '0' isSplitEvent= 'False' isViewed= 'False' itemId= 'BOOK:688059918' itemParentid= '3' itemRestricted= '0' lastPlaybackPosition= '0' lastViewedTime= '19700101T01:00:00' marketingMsg= 'None' oigProgId= '0' parentServiceLocator= 'None' parentalRatingScheme= '1' pdlDownloadStatus= '0' pdlDownloadStatusName= 'not applicable' pdlPlaybackAvailable= 'False' pdlQueuePosition= '0' pinRating= '0' pinRatingScheme= '1' programID= 'xsi://FE2;1A' programIDType= 'nds.com_URI' purchaseType= '0' pushExpiryEnd= 'None' pushExpiryStart= 'None' pushHasValidTrailer= 'True' pushLocator= 'None' pushPublishTime= 'None' rating= '0' ratingName= 'Unrated' ratingType= 'nds.com_internal' recStatus= '3' recStatusContentstatus= '3' recStatusContentstatusName= 'All' recStatusException= '100' recStatusFailed= 'False' recStatusName= 'Currently recording' recStatusRecstate= '4' recordedDuration= 'None' recordedStartDateTime= '20170208T10:58:00' recordingID= 'xsi://FE2;1A' reminderStatus= '1' reminderStatusIsvcrtimer= 'False' reminderStatusName= 'event is scheduled for a reminder' remoteCDSItemID= 'None' res= 'file://pvr/2902F60E' resDuration= 'None' resProtocolinfo= 'internal:10.10.8.40:*:*' resSize= '0.0' scheduledDuration= 'P0D01:00:00' scheduledEndTime= '20170208T12:00:00' scheduledStartTime= '20170208T11:00:00' seasonNumber= '0' seriesID= '57878' seriesInfo= 'None' serviceType= '16' serviceTypeName= 'HD digital TV' srsRecordTaskID= 'RT:2902F60E' subGroupID= '0' title= 'Cold Case' totalChildSize= '0.0'

我想在“res =”之后提取数据。所以,我只需要这些数据:'file:// pvr / 2902F60E'

if "res=" in last_event:

print "True"
m = re.search('res=\W\S+', last_event)
m.group(0)
print m

输出结果为:

真 < _sre.SRE_Match对象位于0x0268D5D0>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

只需使用()捕获群组(m.group(0)打印整个字符串,您不需要res=或文字前面的前导空格

m = re.search(r'res=\W(\S+)', last_event)
if m: print(m.group(1))

顺便说一下,我怀疑在你的整套线路上应用re.findall,所以你得到一个匹配列表而不是逐行扫描,对你的使用来说比对匹配对象&提取

import re

print(re.findall(r'res=\W*(\S+)', lines))

(也容忍=之后的非空格

答案 1 :(得分:0)

你可以去:

import re

rx = re.compile(r"res=\s*'([^']+)'")

try:
    m = rx.search(your_string)
    filename = m.group(1)
    print(filename)
except:
    print("Not found")

请参阅a demo on regex101.com以及ideone.com