How to replace line breaks except if a character appears in that line in Python 3

Viewed 69

I am trying to create a python code that will delete line break (\n) if a character (<) does not appear in that line. Currently I cannot see any effects. This is the code i wrote so far:

protein=open(r"string.txt","r+")

def main():

    print(protein.readlines())
    print()
    rewrite()


def rewrite():

    for line in protein:

        if line == "<":
            print (line)
        if line != "<":
            line.replace("\n"," ") 

    print(protein)


    protein.close


main()

Input Example:

Name1<<
Data1
Data1
Data1
Name2<<
Data2
Data2
Data2

Expected Output Example:

Name1<<
Data1Data1Data1
Name2<<
Data2Data2Data2
5 Answers

This is the below code you asked

file_handle = open("string.txt", "r")
protein = file_handle.readlines()

final_string = ""
for line in protein:
    if "<" in line:
        final_string += line
    else:
        final_string += line.strip()
print(final_string)

file_handle.close()

But this produces the result as

Name1<<
Data1Data1Data1Name2<<
Data2Data2Data2

But if you want the desired output, use the below code

file_handle = open("string.txt", "r")
protein = file_handle.readlines()

final_string = ""
for line in protein:
    if "<" in line:
        final_string += "\n" + line
    else:
        final_string += line.strip()
print(final_string.strip())

file_handle.close()

# if you want to update the file content
file_handle = open("string.txt", "w")
file_handle.write(final_string)
file_handle.close()

Output of above code will be

Name1<<
Data1Data1Data1
Name2<<
Data2Data2Data2

Use FileInput

for line in fileinput.FileInput("+some_file+", inplace=1):
...

you can use the inputfile module to perform inplace editing of the file data. Assuming the first line will be name line so just print it then for each additional line if there is no << at the end then print the line without a new line char, otherwise print a new line char then the line

import fileinput
with fileinput.input('query4.txt', inplace=True) as protein:
    print(protein.readline(), end='')
    for line in protein:
        line = line.rstrip()
        if line.endswith('<<'):
            print("\n" + line)
        else:
            print(line, end='')

This seems to work

protein=open(r"string.txt","r+")

def main():
    rewrite()

def rewrite():
    s = ""
    nl = False
    for line in protein.readlines():
        if "<" in line:
            if nl:
                s += '\n'
            nl = False
            s += line
        else:
            s += line.replace('\n', '')
            nl = True
    print(s)

    protein.close()

main()

Here is another way to achieve your goal.

def rewrite(protein):
    results = ""
    for line in protein:
        if "<" in line:
            results += "\n" + line
        else:
            line = line.replace("\n", "")
            results += line
    print(results)

def main():
    f = open(r"string.txt","r+")
    protein = f.readlines()
    rewrite(protein)
    f.close()


main()

Output:

Name1<<
Data1Data1Data1
Name2<<
Data2Data2Data2
Related