'Convert a Python block of code into a single line

Below is the python block code,

def compute_hcf(x, y):
   while(y):
       x, y = y, x % y
   return x

which I wanted to convert into single line like below,

def compute_hcf(x, y):\n\t while(y): \n\t\t x, y = y, x % y \n\t return x

I've the below approach to solve this,

def convert(fname):
    newLines = ''
    with open(fname, 'r') as f:
        for line in f:
            num_spaces = 0
            
            for letter in line:
                if letter != ' ':
                    break
                num_spaces += 1

            if num_spaces == 3:
                line = line.replace(' '*3, '\\t') #If number of Spaces is 3, replace with a single tab
            elif num_spaces == 4:
                line = line.replace(' '*4, '\\t') #If number of Spaces is 4, replace with a single tab
            elif num_spaces == 7:
                line = line.replace(' ' *7, '\\t\\t') #If number of Spaces is 7, replace with double tab
            elif num_spaces == 8:
                line = line.replace(' ' * 8, '\\t\\t') #If number of Spaces is 8, replace with double tab
            line = line + '\\n' #Adding new line at the end of the line
            newLines = newLines + line
    converted = ''.join(newLines)
    print(converted)
    
convert('fileWithInputContent.txt')

Actual Output:

def compute_hcf(x, y):
\n\twhile(y):
\n\t\tx, y = y, x % y
\n\treturn x\n

Expected Output:

def compute_hcf(x, y):\n\t while(y): \n\t\t x, y = y, x % y \n\t return x


Solution 1:[1]

When you read text from a file in python, there is automatically a newline character added to the end of each line. So when we print out the strings that python reads it looks like this.

image showing that there is a newline character at the end of the string

This is why the actual output has newlines. So in order to fix this, we just need to remove the newline character with something like this.

if '\n' in line:
     line = line.replace('\n', '')

The code with this addition looks like this.

def convert(fname):
newLines = ''
with open(fname, 'r') as f:
    for line in f:
        # print each string
        # print(repr(line))
        if '\n' in line:
            line = line.replace('\n', '')
        num_spaces = 0
        
        for letter in line:
            if letter != ' ':
                break
            num_spaces += 1

        if num_spaces == 3:
            line = line.replace(' '*3, '\\t') #If number of Spaces is 3, replace with a single tab
        elif num_spaces == 4:
            line = line.replace(' '*4, '\\t') #If number of Spaces is 4, replace with a single tab
        elif num_spaces == 7:
            line = line.replace(' ' *7, '\\t\\t') #If number of Spaces is 7, replace with double tab
        elif num_spaces == 8:
            line = line.replace(' ' * 8, '\\t\\t') #If number of Spaces is 8, replace with double tab
        line = line + '\\n' #Adding new line at the end of the line
        newLines = newLines + line
        
converted = ''.join(newLines)
print(converted)

And it works!

def compute_hcf(x, y):\n\twhile(y):\n\t\tx, y = y, x % y\n\treturn x\n

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akash Dubey