Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读入行间数组_Python_Arrays_Readline_File Handling - Fatal编程技术网

Python 读入行间数组

Python 读入行间数组,python,arrays,readline,file-handling,Python,Arrays,Readline,File Handling,有没有一种方法可以使用某些模式作为“屏障”将文件的一部分读取到数组中 编辑:经过一点搜索和混乱,我已经管理了[INTERNAMITE]和[depotNum]之间的行 但在打印时,它的作用如下: # intermediate dir (output dir of route chopper), is route files and # common files for each truck ready for customer format processing PX12RUJ = /

有没有一种方法可以使用某些模式作为“屏障”将文件的一部分读取到数组中

编辑:经过一点搜索和混乱,我已经管理了[INTERNAMITE]和[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        
你可以试试这种东西

我会用:

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)

那么您自己尝试了什么呢?答案部分取决于文件中的行是否已排序且唯一(您发布的内容未排序)。@RvdK添加了代码和输出数据,对延迟表示歉意我尝试了您的解决方案,但唯一返回的是[]这是我的regex
打印re.findall(\[intermediate\].+(?=\[deptnum\])”,FILE.read())“
有什么想法我哪里出错了吗?
import re
f=open("file.txt",'r')
print re.findall(r""+start_pattern+"[\s\S]+?"+end_pattern",f.read())
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)