一尘不染

Python - 将行附加到具有特定条件的输出文件时出现问题

py

我的问题如下:

我有一个文件,其中的行通常以“ab”开头,条件是当行不是以 ab 开头时,它应该附加到上一行,但有些行没有附加到输出文件

Source File:
grpid;UserGroup;Name;Description;Owner;Visibility;Members -> heading
ab;user1;name1;des1;bhalji;public
sss
ddd
fff
ab;user2;name2;des2;bhalji;private -> not appended in output

ab;user3;name3;des3;bhalji;public -> not appended in output

ab;user4;name4;des4;bhalji;private
rrr
ttt
yyy
uuu

ab;user5;name5;des5;bhalji;private
ttt
ooo
ppp

这是我正在使用 python 做的事情:

def grouping():
    output = []
    temp = []
    currIdLine = ""
    with( open ('usergroups.csv', 'r')) as f:
        for lines in f.readlines(): 
            line = lines.strip()
            if not line:
               print("Skipping empty line")
               continue 
            if line.startswith('grpid'): 
               output.append(line)
               continue 
            if line.startswith('ab'):
                if temp:
                   output.append(currIdLine + ";" + ','.join(temp))
                   temp.clear()
                currIdLine = line
            else:
                temp.append(line)
    output.append(currIdLine + ";" + ','.join(temp))

        #print("\n".join(output))

        with open('new.csv', 'w') as f1:
            for row in output:
                f1.write(row + '\n')

grouping ()
Output of the above code:
grpid;UserGroup;Name;Description;Owner;Visibility;Members
ab;user1;name1;des1;bhalji;public;sss,ddd,fff
ab;user4;name4;des4;bhalji;private;rrr,ttt,yyy,uuu
ab;user5;name5;des5;bhalji;private;ttt,ooo,ppp

我希望这对 Python 来说应该很容易,但到目前为止我还没有做对。

这就是文件最后的样子:

Expected Output:
grpid;UserGroup;Name;Description;Owner;Visibility;Members
ab;user1;name1;des1;bhalji;public;sss,ddd,fff
ab;user2;name2;des2;bhalji;private
ab;user3;name3;des3;bhalji;public
ab;user4;name4;des4;bhalji;private;rrr,ttt,yyy,uuu
ab;user5;name5;des5;bhalji;private;ttt,ooo,ppp

阅读 97

收藏
2023-01-27

共1个答案

一尘不染

你很接近。缺少一个else语句来处理本行以ab开头,而上一行以ab开头的情况。

def grouping():
    output = []
    temp = []
    currIdLine = ""
    with(open('usergroups.csv', 'r')) as f:
        header = f.readline()
        output.append(line.strip())
        temp = []
        for line in f.readlines():
            if not line:
                print("Skipping empty line")
                continue
            if line.startswith('ab'):
                if temp:
                    output.append(currIdLine + ";" + ','.join(temp))
                    temp = []
                else:
                    output.append(currIdLine)
                currIdLine = line.strip()
            else:
                temp.append(line.strip())
    if temp:
        output.append(currIdLine + ";" + ','.join(temp))
    else:   # <-- this block is needed
        output. Append(currIdLine)


    with open('new.csv', 'w') as f1:
        for row in output:
            f1.write(row + '\n')
2023-01-27