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 line
variable. 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 the file
module with a variable, so I renamed that one too.
import re
import sys
filename = "C:\Dropbox\Reference.brf"
try:
input_file = open(filename ,'r')
except IOError as exc:
print exc
else:
cached_line = ""
for line in input_file:
if 'ToolVersionEdit' in line:
cached_line = line.split('=')[1]
break
print cached_line
Use ConfigParser. Construction of your file is unix config file and ConfigParser (configparser in python3) can read and parse it http://docs.python.org/2/library/configparser.html .
from ConfigParser import RawConfigParser as Parser
filename = 'yourfile.conf'
config = Parser()
config.read(filename)
print config.get('BuildRequest', 'ToolVersionEdit')
if you want you can get all options by this code:
for section in config.sections():
for option, value in config.items(section):
print value
您要使用split
而不是strip
You need actually two things.
strip
to split
break
after you split it. Resultant code looks like this,
7 for line in lines:
8 if 'ToolVersionEdit' in line:
9 line = line.split('=')[1]
10 break
Try this:
import re
import sys
file = "/tmp/abc.txt"
lines = open(file ,'r').readlines()
for line in lines:
if 'ToolVersionEdit' in line:
desired_line = line.split('=')[1]
print desired_line
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.