Hi, I need to extract some info from a file. This is two rows from what the file looks like:

prob=0.0093;ID=RGT430;BQRS=491;BRZ=-4.263;ID2=RT914;DRT=0.00;HRun=0;HaplotypeScore=0.2794;
prob=0.003;ID=RGR301;BQRS=4;BRZ=-3.261;ID2=EV913;DRT=0.00;HRun=0;HaplotypeScore=0.2654;
....etcetc until last row:
prob=0.345.

I want to extract prob from each row in the file and put it in a new file, but I don't know how to do it. I tried to use index to find each prob, but the thing is that the last row ends with prob=0.0093. , i.e. prob=0.0093 with a dot at the end, so when I search for 'index_end' which would be semicolon it works for all lines except the last one, which is a dot.

Still with this? :(

data = """prob=0.0093;ID=RGT430;BQRS=491;BRZ=-4.263;ID2=RT914;DRT=0.00;HRun=0;HaplotypeScore=0.2794;
prob=0.003;ID=RGR301;BQRS=4;BRZ=-3.261;ID2=EV913;DRT=0.00;HRun=0;HaplotypeScore=0.2654;
....etcetc until last row:
prob=0.345."""

def prob(line):
    if 'prob=' in line:
            line = line.split('prob=', 1)[1].rstrip('.')
            return float(line.split(';', 1)[0])

for line in data.splitlines():
    pr = prob(line)
    if pr is not None:
        print(pr)

Just test for it and eliminate the trailing period

test_text="prob=0.345."
test_text = test_text.strip()
if test_text.endswith("."):
    print test_text[:-1]
    print test_text[:-1].split("=")

thank you so much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.