Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arrange lines in python
#1
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.
Reply
#2
Show what you have done so far, and where you need help
Reply
#3
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()
Reply
#4
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 ...
Reply
#5
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.
Reply
#6
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)
Reply
#7
Any help here?
Reply
#8
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.
Reply
#9
This one is a text file.
Reply
#10
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']}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What are these python lines for? What are tey doing? Led_Zeppelin 7 1,673 Feb-13-2023, 03:08 PM
Last Post: deanhystad
  python seems to be skipping lines of code alansandbucket 1 4,220 Jun-22-2021, 01:18 AM
Last Post: Larz60+
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 6,039 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Arrange my looped template data in rows JedBoyle 0 1,577 Feb-19-2020, 01:41 PM
Last Post: JedBoyle
  Arrange list of tuple using loop batchenr 7 3,549 Jun-16-2019, 03:24 PM
Last Post: Abdullah
  How to re run lines in python? MrDoggo124 5 4,628 May-19-2019, 06:29 PM
Last Post: MrDoggo124
  Re-arrange lines when adding new if statement bghayad 3 2,879 Mar-21-2018, 07:29 PM
Last Post: nilamo
  parallel(offset) lines using python johnfriend 1 4,320 May-05-2017, 06:10 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020