So I have a file that imports large amounts of data from a vision system. It saves the data in a text file and there are about 4000 lines of text with 1 line per result. I will add 1 of these lines below as an example.
11/02/1970; 11:56:44.000;ID;002914;Light Check;254;Tube Width1;38.7;Tube Width2;39.2;Tube Width3;39.9;Tube Width4;40.9;Tube Width5;41.2;Fixt Row;175.20;Fixt Col;211.23;Post Width;0.00;Blob Size;0;Left Angle;0.00;Right Angle;17.90;Dark Blob;0;Result;0;Global St;14;Tool Flag;31;Pallet No; 108;
So what I want to do is be able to do is extract for each line one of the parameters along with its value. There is a delimiter of ; between every space which is making it difficult for me.
So if I for example wanted to choose Light Check, I would get the results of Light Check for each line which in this case is 254. Can someone suggest some functions I could use that could possibly help me with this?
>Solution :
I suggest using regular expression (re module here), let file.txt content be
11/02/1970; 11:56:44.000;ID;002914;Light Check;254;Tube Width1;38.7;Tube Width2;39.2;Tube Width3;39.9;Tube Width4;40.9;Tube Width5;41.2;Fixt Row;175.20;Fixt Col;211.23;Post Width;0.00;Blob Size;0;Left Angle;0.00;Right Angle;17.90;Dark Blob;0;Result;0;Global St;14;Tool Flag;31;Pallet No; 108;
then
import re
with open("file.txt","r") as f:
for line in f:
print(re.search(r"Light Check;([0-9]+)",line).group(1))
output
254
Explanation: I iterate over following lines (for line in f, thus there is no need to load whole file into memory), then in each line I find 1 or more (+) digits ([0-9]) after Light Check;. Note digits are inside ( and ) that is capturing group (first and only) which I access using group(1). Disclaimer: this solution assumes Light Check; followed by 1 or more digits is present in each line of file.txt.