jeudi 15 août 2019

checking for multiple conditions in python

I have a text file and some where in that file I have a line with the following content

xxxx xxxx y = 4.63456

where xxxx represents the part the line I am not interested in. My goal is to extract that y = 4.63456 value and write it to a new text file. Here is what I have so far.

import os
import re


my_absolute_path = os.path.abspath(os.path.dirname(__file__))

with open('testfile', 'r') as helloFile, open('newfile32','a') as out_file:

 for line in helloFile:

     numbertocheck= []

     if 'x' in line and ' = ' in line and numbertocheck==type(float) in line:

        out_file.write(line)

The code creates the file but the file is empty. Is this the right way of checking for the conditions in the if statement? FWIW, if i removed the two conditionals at the end and wrote if x in line: the code works fine but prints out the entire line.

Aucun commentaire:

Enregistrer un commentaire