Skip to content

Instantly share code, notes, and snippets.

@amzar96
Last active October 17, 2024 01:58
Show Gist options
  • Save amzar96/f49967d041bfb72ddf0eefd9682eb545 to your computer and use it in GitHub Desktop.
Save amzar96/f49967d041bfb72ddf0eefd9682eb545 to your computer and use it in GitHub Desktop.
text to comma-separated string converter (sh file)

Text to Comma-Separated String Converter

Setup

  1. Ensure Python is installed on your system.
  2. Save convert-text-list.py and convert-to-list.sh in the same directory.
  3. Make the bash script executable:
    chmod +x convert-to-list.sh
    

Usage

Run the converter with:

./convert-to-list.sh your_input_file.txt

Replace your_input_file.txt with the path to your input file.

import sys
def convert_list_to_string(filename):
try:
with open(filename, "r") as file:
lines = file.read().splitlines()
result = ",".join(f"'{line}'" for line in lines)
print(f"({result})")
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <filename>")
else:
convert_list_to_string(sys.argv[1])
#!/bin/bash
# Define the path to the Python script using the home directory
PYTHON_SCRIPT="$HOME/Documents/convert-text-list.py"
# Check if a filename is provided
if [ $# -eq 0 ]; then
echo "Error: No input file specified."
echo "Usage: $0 <input_file>"
exit 1
fi
# Input file
input_file="$1"
# Check if the input file exists
if [ ! -f "$input_file" ]; then
echo "Error: File '$input_file' not found."
exit 1
fi
# Check if the Python script exists
if [ ! -f "$PYTHON_SCRIPT" ]; then
echo "Error: Python script '$PYTHON_SCRIPT' not found."
exit 1
fi
# Run the Python script
python "$PYTHON_SCRIPT" "$input_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment