I have a file with the following content:
&ACCESS RVP1
&REL 3
&PARAM DISKPATH = KRC:\R1\Program\new
DEF SETUP_WELD()
;call LASER_EN();
$out[27]=false; START=OFF
$out[26]=false; STROBE=OFF
$out[25]=false; SELECT 8=OFF
$out[24]=false; SELECT 7 =OFF
$out[23]=false; SELECT 6 =OFF
$out[22]=false; SELECT 5 =OFF
$out[21]=false; SELECT 4 =OFF
$out[20]=false; SELECT 3 =OFF
$out[19]=false; SELECT 2 =OFF
$out[18]=false; SELECT 1 =OFF
$out[17]=false; SELECT 0 =OFF
$out[28]=true; ENABLE=ON
END
I need to find the value,that in the brackets [], and write it into array. In result, I should get next result :
[ '27', '26','25','24','23','22','21','20','19','18','17','28']
I have to do it using Python. I'm pretty new to this, could you , please, , give me a hint what is the most proper way to do it?
My idea was something like this: I read the file into array,and then I was thinking about using the search in element of array:
def reading():
with open ('Setup_Weld.src') as f:
array1=[row.strip() for row in f]
But I don't know how to search in elements of array.
UPD: The answer was found. The working code is:
def reading():
with open ('Setup_Weld.src') as f:
stripped=[row.strip() for row in f]
setup_weld_num = [re.search(r'\[(.*?)\]',i).group(1) for i in stripped if re.search(r'\[(.*?)\]',i)]
print(setup_weld_num)
reading()
Use regex
import re
with open(file_name) as file_object:
data=file_object.read().replace('\n', '')
re.findall(r"\[(.*?)\]",data)
try regex this is what you want,
import re
def reading():
with open ('Setup_Weld.src') as f:
stripped=[row.strip() for row in f]
my_num_list = [re.search(br'\[(.*?)\]',i).group(1) for i in stripped if re.search(br'\[(.*?)\]',i)]
print(my_num_list)
reading()
python3 output,
mohideen@botvfx-dev:~/Desktop$ python3 run.py
[b'27', b'26', b'25', b'24', b'23', b'22', b'21', b'20', b'19', b'18', b'17', b'28']
mohideen@botvfx-dev:~/Desktop$
here is my output,
Since the format is so simple, you can do this without regex, just using Python's string methods. Here's an adaptation of your start:
def reading():
with open ('Setup_Weld.src') as f:
array1=[row.strip() for row in f]
return [line[5:].split(']')[0] for line in array1]
This removes the first four characters of each line, and then keeps the part before the closing bracket.
Output:
['27', '26', '25', '24', '23', '22', '21', '20', '19', '18', '17', '28']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.