[英]Finding the digits in text
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. 我必须使用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. UPD:找到了答案。 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, python3输出,
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. 由于格式非常简单,因此您无需使用正则表达式就可以执行此操作,只需使用Python的字符串方法即可。 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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.