![]() |
Arrange lines in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Arrange lines in python (/thread-4127.html) Pages:
1
2
|
Arrange lines in python - pythonlover - Jul-24-2017 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. RE: Arrange lines in python - Larz60+ - Jul-24-2017 Show what you have done so far, and where you need help RE: Arrange lines in python - pythonlover - Jul-27-2017 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() RE: Arrange lines in python - Larz60+ - Jul-27-2017 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 ... RE: Arrange lines in python - pythonlover - Jul-27-2017 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. RE: Arrange lines in python - pythonlover - Jul-31-2017 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) RE: Arrange lines in python - pythonlover - Aug-08-2017 Any help here? RE: Arrange lines in python - nilamo - Aug-09-2017 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. RE: Arrange lines in python - pythonlover - Aug-10-2017 This one is a text file. RE: Arrange lines in python - nilamo - Aug-10-2017 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)
|