DEV Community

Cover image for How to Add Quotes and Commas to Each Line in a Text File Using Python
Vicente G. Reyes
Vicente G. Reyes

Posted on • Originally published at blog.vicentereyes.org

How to Add Quotes and Commas to Each Line in a Text File Using Python

Processing text files is a common task in programming, whether it’s for data cleaning, preparation, or formatting. In this tutorial, we’ll explore how to use Python to modify a .txt file by adding double quotes (") around each line and a comma (,) at the end.

This step-by-step guide will help you process your text file efficiently, regardless of its size.

The Task

Suppose you have a .txt file containing 5159 lines, where each line represents a phrase. Your goal is to:

  1. Add double quotes (") around the phrase on each line.

  2. Append a comma (,) at the end of each line.

  3. Save the modified lines into a new file.

Example

Input File (input.txt):

Hello
World
Python
Enter fullscreen mode Exit fullscreen mode

Desired Output File (output.txt):

"Hello",
"World",
"Python",
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Solution

Here’s how you can accomplish this task using Python.

  1. Read the Input File

The first step is to read the contents of the .txt file. Python’s built-in open() function allows you to easily read lines from a file.

  1. Process Each Line

Using Python’s string formatting, we’ll add the required double quotes and comma to each line.

  1. Write to the Output File

Finally, write the processed lines into a new file to preserve the original data.

Complete Python Code

Below is the complete Python script to perform the task:

# File paths
input_file = "input.txt"  # Replace with your input file path
output_file = "output.txt"  # Replace with your desired output file path

# Step 1: Read the input file
with open(input_file, "r") as file:
    lines = file.readlines()

# Step 2: Process each line
processed_lines = [f'"{line.strip()}",\n' for line in lines]

# Step 3: Write to the output file
with open(output_file, "w") as file:
    file.writelines(processed_lines)

print(f"Processed {len(lines)} lines and saved to {output_file}.")
Enter fullscreen mode Exit fullscreen mode

Explanation of the Code

Reading the File

with open(input_file, "r") as file:
    lines = file.readlines()
Enter fullscreen mode Exit fullscreen mode
  • This opens the file in read mode ("r") and reads all lines into a list called lines.

Processing Lines

processed_lines = [f'"{line.strip()}",\n' for line in lines]
Enter fullscreen mode Exit fullscreen mode
  • line.strip() removes any leading or trailing whitespace or newline characters from each line.

  • f'"{line.strip()}",\n' formats each line by wrapping it in double quotes and appending a comma, followed by a newline character (\n).

Writing to the File

with open(output_file, "w") as file:
    file.writelines(processed_lines)
Enter fullscreen mode Exit fullscreen mode
  • This opens the output file in write mode ("w") and writes the processed lines to it.

Running the Script

  1. Save the script to a .py file, for example, process_text.py.
  2. Place your input file (input.txt) in the same directory as the script, or update the file paths in the code.
  3. Run the script using Python:
python -m process_text
Enter fullscreen mode Exit fullscreen mode
  1. Check the output.txt file for the results.

Example Output

If your input file contains:

Hello
World
Python
Enter fullscreen mode Exit fullscreen mode

The output file will look like:

"Hello",
"World",
"Python",
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using Python, you can quickly and efficiently modify text files, even when they contain thousands of lines. The code is simple, reusable, and can be adapted to perform other text-processing tasks.

This solution is particularly useful for preparing data for programming tasks, such as importing data into a database or generating structured formats like JSON or CSV. With Python, the possibilities are endless!

Top comments (0)