Advertisement
Python253

to_snake_case

May 23rd, 2024
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: to_snake_case.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script converts input text to Snake Case, where spaces are replaced with underscores ('_').
  10.    The converted text is then saved to a file named "converted_snake_case.txt" in UTF-8 encoding.
  11.  
  12. Requirements:
  13.    - Python 3.x
  14.  
  15. Functions:
  16.    - to_snake_case(text): Converts input text to Snake Case.
  17.        Parameters:
  18.            text (str): The input text to be converted.
  19.        Returns:
  20.            str: The text converted to Snake Case.
  21.  
  22. Usage:
  23.    Run the script and provide the text you want to convert when prompted.
  24.    The converted text will be saved to "converted_snake_case.txt" in the same directory as the script.
  25.  
  26. Example Output:
  27.    Input Text: "This is a Test of Case Converting"
  28.    
  29.    Converted Text Has Been Saved To: 'converted_snake_case.txt'.
  30.    
  31.    Converted Text:
  32.            this_is_a_test_of_case_converting
  33. """
  34.  
  35. def to_snake_case(text):
  36.     """
  37.    Converts input text to Snake Case.
  38.    
  39.    Parameters:
  40.        text (str): The input text to be converted.
  41.    
  42.    Returns:
  43.        str: The text converted to Snake Case.
  44.    """
  45.     return '_'.join(text.lower().split())
  46.  
  47. def main():
  48.     """
  49.    Main function of the script.
  50.    
  51.    Prompts the user to input text, converts it to Snake Case,
  52.    and saves the converted text to a file named "converted_snake_case.txt".
  53.    """
  54.     input_text = input("Enter the text you want to convert to Snake Case: ")
  55.     snake_case_text = to_snake_case(input_text)
  56.  
  57.     with open("converted_snake_case.txt", "w", encoding="utf-8") as file:
  58.         file.write(snake_case_text)
  59.    
  60.     print(f"\nConverted Text Has Been Saved To: 'converted_snake_case.txt'.\n\nConverted Text:\n\t\t{snake_case_text}")
  61.  
  62. if __name__ == "__main__":
  63.     main()
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement