I am trying to read an input file and match the line which contains “ToolVersionEdit” and then split based on “=” and get the second part..I am using the below..am not getting the desired output..where am I going wrong?input and expected ouput are given below
INPUT:
[BuildRequest]
BuildRequestVersion=4.4.21
BuildRequestType=Phone
BuildCommandComboBox=common/build/build.sh tz:A8064AAAAANAAT140029.1 tz_bid=AAAAANAA wcnss:A8064AAAAANAAW120072.1 wcnss_bid=SCAQBAF lpass:A8064AAAAANAZL140106.1 boot_9x15:M9615ACETRMAAB12171.1 boot_9x15_bid=ACEHRMAA rpm:A8064AAAAANAAR1100153.1 rpm_bid=AAAAANAAR modem_9x15:M9615ACEFWTAAM4010223.1 modem_9x15_bid=ACEFWTAA apps_9x15:M9615AFEHRMAA2745.1 apps_9x15_bid=AFEHRMAA rpm_9x15:M9615ACETRMAAR1100159.4 rpm_9x15_bid=AAAAANAAR boot:A8064AAAAANAAB12171.1 boot_bid=AAAAANAA lpass_9x15:M9615ACETRMAZL140105.3 apps:A8064AAAAANLGA2214074.1 dsps:A8064AAAAANAAS150007.1 dsps_bid=DSPSBLD
ToolVersionEdit=1.6.21
CheckSumCheckBox=0
PurposeEdit=
[BuildRequestComments]
LineCount=0
EXPECTED OUTPUT:-1.6.21
import re
import sys
file = "C:\Dropbox\Reference.brf"
lines = open(file ,'r').readlines()
for line in lines:
if 'ToolVersionEdit' in line:
line = line.strip('=')[1]
print line
Your problem is that you are overwriting the
linevariable. You’re using the same variable as your looping variable as what you’re trying to cache. Also, you don’t need to continue the loop once you’ve found what you’re looking for.Another change I made was to remove the call to
readlines. You can iterate over all the lines directly from the file object. Also, it’s bad form to (potentially) overwrite thefilemodule with a variable, so I renamed that one too.