Home > Mobile >  Extract lines from multiple text files
Extract lines from multiple text files

Time:01-14

I have over 200 text files (2021001.cnv - 2021260.cnv) with measurement and calibration data. I would like to extract lines that start with "time=" and "value2=" from every file and append those to summary text file:

time= 2021/01/01 21:12
value2= 32.6435
time= 2021/04/08 16:25
value2= 65.3452
time= 2021/02/03 18:33
value2= 23.4543

So far I have tried to used this code to find "time=" and "value2=".

import re

errors = []
linenum = 0
pattern1 = re.compile(r"time=")
pattern2 = re.compile(r"value2=")

with open ('a1.cnv', 'rt') as myfile:
   for line in myfile: linenum  = 1
       if pattern1.search(line) != None:
           errors.append((linenum, line.rstrip('\n')))
       if pattern2.search(line) != None:
           errors.append((linenum, line.rstrip('\n')))
   for err in errors: print(err[1])

CodePudding user response:

You can make a list for all the files and use a for loop to read lines with pandas or open(). You can store the output for each file in a new list and make a new csv file containing the output.

CodePudding user response:

Create a new list to store the lines you find. Use the open() function to open the file and call readlines() on the result. then iterate over those lines and use regex to match lines that contain "time=" and "value2=". If it matches, add the line to your list. Repeat for each file. Once you have processed all the files, write the new list of lines to a new file

CodePudding user response:

You can use python string method startswith(). Here is the documentation.

After you open the file you can run the following code:

for line in opened_file: linenum  = 1
      if line.startswith("time=") or line.startswith("value2="):
            errors.append((linenum, line.rstrip('\n')))         
  •  Tags:  
  • Related