pyton code testing in online tool and applying in cloudlab for large files and make generic
testing pyton code in online browser.
applying in cloud lab.
vertex code:
question and answer
import re
# Input text
text = """
Line 1
Line 2
Line 3
line4
line1
line2
"""
# Define a function to apply the transformation logic
def process_text(text):
# Check if the text starts and ends with \n\n
if text.startswith('\n\n') and text.endswith('\n\n'):
# Extract the inner content between the starting and ending \n\n
inner_content = text[2:-2]
# Replace all single newlines \n inside the content with space
modified_content = inner_content.replace('\n', ' ')
# Return the modified content with the original \n\n at the start and end
return f'\n\n\n\n'
return text
# Apply the logic to the normalized content
modified_text = process_text(text)
print(modified_text)
--- is not working, i want to see , single line in between lines in \n\n anyting \n\n
----
import re
def process_text(text):
"""Replaces single newlines within double-newline delimited blocks with spaces."""
# Use re.split to split the text into blocks separated by two or more newlines
blocks = re.split(r'\n{2,}', text)
# Process each block individually
processed_blocks = []
for block in blocks:
# Remove leading/trailing single newlines within the block
block = block.strip('\n')
# Replace single newlines with spaces within the block
processed_block = block.replace('\n', ' ')
processed_blocks.append(processed_block)
# Join the processed blocks with double newlines
return '\n\n'.join(processed_blocks)
text = """
Line 1
Line 2
Line 3
line4
line1
line2
"""
modified_text = process_text(text)
print(modified_text)
------------
import re
# Define the input and output file paths
input_file_path = '/content/Cloud_స్వర్గము ఇంద్ర లోకమా_1stOct24_Updated.txt' # Upload your input.txt file to the content folder
output_file_path = '/content/Cloud_స్వర్గము ఇంద్ర లోకమా_1stOct24_Updated_output.txt'
# Read the content of the input file
with open(input_file_path, 'r') as file:
text = file.read()
# Define a function to apply the transformation logic
def process_text(text):
leading_newlines = len(re.match(r'^\n+', text).group(0)) if re.match(r'^\n+', text) else 0
trailing_newlines = len(re.search(r'\n+$', text).group(0)) if re.search(r'\n+$', text) else 0
inner_text = re.sub(r'^\n+|\n+$', '', text) #remove leading/trailing newlines before splitting
blocks = re.split(r'\n{2,}', inner_text)
processed_blocks = []
for block in blocks:
processed_block = block.replace('\n', ' ')
processed_blocks.append(processed_block)
result = '\n\n'.join(processed_blocks)
return '\n' * leading_newlines + result + '\n' * trailing_newlines
# Apply the logic to the entire content
modified_text = process_text(text)
# Write the modified content to the output file
with open(output_file_path, 'w') as file:
file.write(modified_text)
print(f"Modified file has been saved to {output_file_path}")