python通过写入文件保留缩进

时间:2014-08-24 15:55:56

标签: python

我有一个python代码,它将对paranthesis中存在的相同单词进行分组,并将剩余的文本组合在一起。但问题是它在写入后不保留缩进。

我的代码:

import re
import collections
class Group:
    def __init__(self):
        self.members = []
        self.text = []

with open('text.txt','r+') as f:
    groups = collections.defaultdict(Group)
    group_pattern = re.compile(r'^(\S+)\((.*)\)$')
    current_group = None
    for line in f:
        line = line.strip()
        m = group_pattern.match(line)
        if m:    # this is a group definition line
            group_name, group_members = m.groups()
            groups[group_name].members += filter(lambda x: x not in groups[group_name].members , group_members.split(','))
            current_group = group_name
        else:
            if (current_group is not None) and (len(line) > 0):
                groups[current_group].text.append(line)
    f.seek(0)
    f.truncate()

    for group_name, group in groups.items():
        f.write("%s(%s)" % (group_name, ','.join(group.members)))
        f.write( '\n'.join(group.text) + '\n')

的text.txt

  Car(skoda,audi,benz,bmw)
    The above mentioned cars are sedan type and gives long rides efficient
 ......

  Car(Rangerover,audi,Hummer)
SUV cars are used for family time and spacious.

预期产出:

  Car(skoda,audi,benz,bmw,Rangerover,Hummer)
    The above mentioned cars are sedan type and gives long rides efficient
 ......
SUV cars are used for family time and spacious.

但是得到:

Car(skoda,audi,benz,bmw,Rangerover,Hummer)The above mentioned cars are sedan type and gives long rides efficient
 ......
SUV cars are used for family time and spacious.

2 个答案:

答案 0 :(得分:0)

您正在使用以下行删除前导和尾随空格:

line = line.strip()

答案 1 :(得分:0)

您的问题在于如何在编写行之前格式化行。

import re
import collections
class Group:
    def __init__(self):
        self.members = []
        self.text = []

with open('text.txt','r+') as f:
    groups = collections.defaultdict(Group)
    group_pattern = re.compile(r'^(\S+)\((.*)\)$')
    current_group = None
    for line in f:
        m = group_pattern.match(line)
        if m:    # this is a group definition line
            group_name, group_members = m.groups()
            groups[group_name].members += filter(lambda x: x not in groups[group_name].members , group_members.split(','))
            current_group = group_name
        else:
            if (current_group is not None) and (len(line) > 0):
                groups[current_group].text.append(line)

    with open("text.txt","w") as f:
        for group_name, group in groups.items():
            f.write("%s(%s)" % (group_name, ','.join(group.members)))
            f.write("\n")
            f.write( ''.join(group.text))
            f.write("\n")