How to read a file line by line if match a string from other file?

Development | Programming languages
Author
Content

How to read a file line by line if match a string from other file? For example a have a log.txt with lines and have a threshold.txt with strings. How to combine together?

Attachments
No attachments

Deadline: 31.01.2030

Comments
autor-image
Senior Python Developer
Python forever!
          with open('data.csv', 'r') as fx1, open('treshold.csv', 'r') as fx2:
              # write header
              f = open('result.csv', 'a')
              header_cfg = "source;collector_stream;collector_name;value;"
              f.write(header_cfg + "\n")
              f.close()
              cf = csv.reader(fx1)
              next(cf) # skip header
              for row in cf:
                  treshline = str(row[0]) 
                  ml0 = [line for line in open('data.csv').readlines() if treshline in line]
                  mlx = str(ml0)
                  ml1 = mlx.replace("[", "")
                  ml2 = ml1.replace("]", "")
                  ml3 = ml2.replace("'", "")
                  f = open('result.csv', 'a')
                  f.write(ml3[:-2]) 
                  f.write("\n") 
                  f.close()


03.01.2021, 14:57:00
-
autor-image
Python programmer
Freelancer
  • 4.5
It works for me:

pattern = 'example'
result = [line for line in open('data.csv').readlines() if pattern in line]
print(result)

03.01.2021, 22:47:28
-
You must be logged for add a new comment and price offer! LOGIN
We use cookies to give you the best possible experience on our site. By continuing to use the site you agree to our use of cookies. Find out more Accept