簡體   English   中英

從文件中讀取並寫入跳過python中某些行的文件

[英]reading from a file and writing to a file skipping certain lines in python

我正在處理的更大代碼問題的一小部分:我正在從文件中讀取內容: glmfile.glm 我正在嘗試將每行從glmfile.glm寫入另一個文件: combined.glm跳過滿足特定條件的行。

glmfile=open("glmfile.glm",'r').readlines()
combined=open("combined.glm",'w')

glmfile.glm的內容如下所示:

...
#other objects
object node {
    name SPU123-389-3066753-3_breaker_rec_recnode;
    phases ABC;
    nominal_voltage 7621.024;
}

object node {
    name SPU123-416-25308_recnode;
    phases ABC;
    nominal_voltage 7621.024;
}
object node {
    name SPU123-403-492320;
    groupid from_db;
    phases CN;
    nominal_voltage 7621.024;
}

object node {
    name SPU123-392-97334;
    groupid from_db;
    phases ABCN;
    nominal_voltage 7621.024;
}

object node {
    name SPU123-391-348982;
    groupid from_db;
    phases AN;
    nominal_voltage 7621.024;
}

object node {
    name SPU123-391-542649;
    groupid from_db;
    phases AN;
    nominal_voltage 7621.024;
}
#few more node objects and other objects
...

現在,我形成了一個node_names數組,如下所示:

node_names=['389-3066753','403-492320','392-97334','391-348982']

我將glmfile的名稱與數組中的元素進行比較,以查看對象節點名是否列在數組node_names

for h,aaline in enumerate(glmfile):
    if aaline.startswith('object node {') and ('SWING' not in glmfile[h+3]):
        if glmfile[h+1][13:-2].strip() in node_names:
            #DO NOT WRITE THAT ENTIRE OBJECT NODE SECTION to 'combined'
            #I am able to skip just that line 'glmfile[h]' but how to NOT   
            #write out the entire object node i.e., glmfile[h:h+6]?
            print glmfile[h:h+6]
        else:
            combined.writelines([glmfile[h:h+6]]) 

注意:我正在努力解決的問題是上面的代碼片段, if case評論。

我們首先談談廣泛的術語,並從那里指定。

你有像這樣的對象:

object node {
    name NAME_VAL;
    phases PHASES_VAL;
    nominal_voltage VOLTAGE_VAL;
}

您正試圖從一個充滿這些對象的文件寫入另一個空白文件,只接受這樣的對象

'SWING' in PHASES_VAL and NAME_VAL in [some list of nodes].

我們這樣做:

import itertools

def grouper(iterable, n, fillvalue=None)
    '''from https://docs.python.org/3/library/itertools.html'''
    args = [iter(iterable)] * n
    return itertools.zip_longest(*args, fillvalue=fillvalue)

with open('path/to/infile.txt') as inf, \
        open('path/to/outfile.txt', 'w') as outf:
    objects_in = grouper(inf, 6) # 5 lines of the object plus a newline
    for obj in objects_in:
        obj = list(obj) # let's look at them all at once
        NAME_VAL = obj[1].strip().split()[1]
        PHASES_VAL = obj[2].strip().split()[1]
        VOLTAGE_VAL = obj[3].strip().split()[1]
        if 'SWING' in PHASES_VAL and \
                NAME_VAL in some_list_of_nodes:
            outf.writelines(obj)

也就是說,如果這是你要一遍又一遍地做的事情,那么為此編寫一個解析器可能會更好。

# node.py

class Node(dict):
    '''simply inheriting from dict will handle almost everything,
    but we will have to give it a classmethod to build from string'''

    @classmethod
    def from_string(cls, s):
        kwargs = {}
        reading = False
        for line in s.splitlines():
            if "}" in line:
                return cls.__init__(**kwargs)
            if reading:
                key,val = line.strip().split()
                kwargs[key]=val
            if "{" in line:
                reading = True

    def __str__(self):
        return "object node {\n" + \
                "\n".join("    {} {}".format(item)\
                          for item in self.items()) + "\n}"

# parser.py

from node import Node

def parse(f):
    tokens = []
    token = []
    parsing = False
    for line in inf:
        if line.startswith('object node {'):
            parsing = True
        if parsing:
            token.append(line.strip())
        if line.strip() == "}":
            parsing = False
            obj = Node("\n".join(token))
            tokens.append[obj]
            token = []
    return tokens

# yourfile.py

import parser

with open('path/to/infile.txt') as infile, \
        open('path/to/outfile.txt', 'w') as outfile:
    for obj in parser.parse(infile):
        if obj['name'] in some_node_names_list and \
               'SWING' in obj['phases']:
            outfile.write(str(obj))

如何使用額外的索引和模運算符:

a%b

例如:

idx = 6
for h,aaline in enumerate(glmfile):
if aaline.startswith('object node {') and ('SWING' not in glmfile[h+3]):
    if glmfile[h+1][13:-2].strip() in node_names or idx%6!=0:
        idx = idx+1
        print glmfile[h:h+6]
        pass
    else:
        combined.writelines([glmfile[h:h+6]]) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM