Reputation: 125
Can someone please check what is wrong with this script. Can "for loops" be used like I have done? I have multiple measurement files (1.cnv, 2.cnv, etc) from where I would like to extract lines starting with "span 1 =" and "cast ", after that I would like to put all those lines to single summary.txt file.
import re
import os
errors = []
linenum = 0
pathin = r'C:\python_test'
flist = [pathin+vv for vv in os.listdir(pathin) if vv.__contains__('.cnv')==True]
find_1 = re.compile(r"span 1 =")
find_2 = re.compile(r"cast ")
for fname in flist:
for line in fname:
linenum += 1
if find_1.search(line) != None:
errors.append((linenum, line.rstrip('\n')))
if find_2.search(line) != None:
errors.append((linenum, line.rstrip('\n')))
for err in errors:
print(err[1])
with open("summary.txt", "a") as myfile: myfile.write(err[1] + "\n")
Upvotes: 0
Views: 995
Reputation: 325
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')))
Upvotes: 2
Reputation: 38
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
Upvotes: 0
Reputation: 69
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.
Upvotes: 0