Python Forum

Full Version: Arrange lines in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello Python Experts,
I was trying to arrange a file in python. If someone can help, that would be great.

My file:

[ Heading 1]
Line 1
Line 2
Line 3

[Heading 2]
Line 1
Line 2
Line 3

[Heading 1]
Line 1
Line 2


Objective: I need to put everything that belongs to heading 1 under a common heading (the second heading 1 should be gone and its contents should be copied under the first heading 1].

This seems fairly simple but can't find a way to figure out. Any help is highly appreciated.
Show what you have done so far, and where you need help
I am just confused on how to do it. How do I insert it to the file when I find a heading match?

def file_manipulation():
    copy = False
    with open("file1.txt", 'r') as inFile, open("file2.txt", 'r+') as outFile:
        for line in inFile:
            if line.startswith("Contact:"):
                copy = True
            if line.startswith("Contact:") and line in outFile:
                copy = False
            if copy:
                outFile.write(line)

    inFile.close()
    outFile.close()
I fixed your indentation. Be careful to do this in future posts.

How large is your file?

Also, remove the close statements, not needed if using with ...
Damn, I thought the indentation was auto managed by the "insert python" button. I will definitely fix that from now on. I removed the close statements. Thanks for pointing that one out. My file is around 500 lines.
This is what I have wrote so far:
1. Copy all the lines that start with heading to a new file.
2. Remove duplicate lines (same heading) on that file and copy it to a new file.
3. Compare the lines in new file to the initial file [Having trouble here]

def file_manipulation():
    outFile2 = open('outFile2', 'w')
    outFile1 = open('outFile1', 'w')
    with open("ownertest.txt", 'r') as inFile, open("ownertest1.txt", 'r+') as outFile:
        for line in inFile:
            if line.startswith("Contact:"):
                outFile.write(line)
                # copy = True
        #remove duplicate contacts [START]
        line1 = outFile.readlines()
        lines_set = set(line1)
        for line1 in lines_set:
            outFile1.write(line1)
        #remove duplicate contacts [END]
        ###############################
    for line2 in outFile1:
        if line2 in inFile:
            print line2
            break
        if line2.startswith("Device"):
            print line2
            break
        outFile2.write(line2)
Any help here?
Is this a particular format?  The headings make it look like it could be a toml file, in which case you could just use a toml parser.
This one is a text file.
Try this on for size:
# faking file-input for simplicity
text = '''
[ Heading 1]
Line 1
Line 2
Line 3

[Heading 2]
Line 1
Line 2
Line 3

[Heading 1]
Line 1
Line 2
'''.split("\n")

import re

# a heading looks like this:
# [heading_name]
# where heading_name can be any combination of characters that isn't a closing bracket
regex = re.compile(r"\[\s*([^\]]+)\s*\]")
sections = {}
current_heading = None
for line in text:
    # ignore blank lines
    if line:
        # see if this line starts a new heading
        match = regex.match(line)
        if match:
            # this line is a heading line
            matches = match.groups()
            if matches and matches[0]:
                current_heading = matches[0]
            # if this is the first time we've seen the heading...
            if current_heading not in sections:
                #...initialize it
                sections[current_heading] = []
        else:
            # this line is non-empty, and does not define a heading
            if current_heading:
                # so add it to whichever heading we're currently processing
                sections[current_heading].append(line)

print(sections)
Output:
E:\Projects\etc>python eggs.py {'Heading 2': ['Line 1', 'Line 2', 'Line 3'], 'Heading 1': ['Line 1', 'Line 2', 'Line 3', 'Line 1', 'Line 2']}
Pages: 1 2