有没有办法使用某些模式将文件的一部分读入数组作为“障碍”
# --------------------
[intermediate] --> STARTING POINT!
# intermediate dir (output dir of route chopper), is route files and
# common files for each truck ready for customer format processing
PX12RUJ = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUJ/
PX12RUR = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUR/
PX12RUV = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUV/
#PX12RUU = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUU/
PX12WLJ = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12WLJ/
#PX12WLL = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12WLL/
PX12WLK = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12WLK/
PX12RUW = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUW/
WN14YGV = /shares/MILKLINK/PPdir/lloydF/tempFiles/WN14YGV/
WN14YGY = /shares/MILKLINK/PPdir/lloydF/tempFiles/WN14YGY/
# --------------------
[depotNum]-->END POINT
编辑:经过一番搜索和搞乱后,我已经管理了[intermediate]和[depotNum]之间的界限
但是在打印时会出现如下情况:
# intermediate dir (output dir of route chopper), is route files and
# common files for each truck ready for customer format processing
PX12RUJ = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUJ/
PX12RUR = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUR/
PX12RUV = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUV/
#PX12RUU = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUU/
PX12WLJ = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12WLJ/
#PX12WLL = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12WLL/
PX12WLK = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12WLK/
PX12RUW = /shares/MILKLINK/PPdir/lloydF/tempFiles/PX12RUW/
WN14YGV = /shares/MILKLINK/PPdir/lloydF/tempFiles/WN14YGV/
WN14YGY = /shares/MILKLINK/PPdir/lloydF/tempFiles/WN14YGY/
# --------------------
这是我的代码,我试图去掉任何以#或空白/空格开头的行,但出于某种原因,它不会削减额外的行
#!/usr/bin/python
FILE=open("/shares/MILKLINK/PPdir/lloydF/conDalt.ini" , 'r')
for LINE in FILE:
LINE = filter(None,LINE)
LINE = filter(lambda x: not x.startswith('#'), LINE)
if LINE.strip() == '[intermediate]':
break
for LINE in FILE:
if LINE.strip() == '[depotNum]':
break
print LINE
答案 0 :(得分:0)
import re
f=open("file.txt",'r')
print re.findall(r""+start_pattern+"[\s\S]+?"+end_pattern",f.read())
你可以试试这种东西
答案 1 :(得分:0)
我会用:
import re
lines_list = []
with open(path,'r') as file:
for line in file.readlines()
if bool(re.match(you_regex, line)):
lines_list.append(line)