Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: txt2csv.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a text file (.txt) to a CSV file (.csv).
- Assumes each line in the text file is a separate record in the CSV file.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'txt2csv.py'.
- 2. Ensure your text file ('example.txt') is in the same directory as the script.
- 3. Run the script.
- 4. The converted CSV file ('txt2csv.csv') will be generated in the same directory.
- Note: Adjust the 'txt_filename' and 'csv_filename' variables in the script as needed.
- """
- import csv
- def txt_to_csv(txt_filename, csv_filename):
- with open(txt_filename, 'r') as txtfile, open(csv_filename, 'w', newline='') as csvfile:
- csvwriter = csv.writer(csvfile)
- for line in txtfile:
- # Assuming each line in the text file is a separate record
- csvwriter.writerow([line.strip()])
- if __name__ == "__main__":
- # Set the filenames for the text and CSV files
- txt_filename = 'example.txt'
- csv_filename = 'txt2csv.csv'
- # Convert the text to a CSV file
- txt_to_csv(txt_filename, csv_filename)
- print(f"Converted '{txt_filename}' to '{csv_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement