Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: csv2txt.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts a CSV file (.csv) to a text file (.txt).
- It reads the CSV content and writes each row as a separate line in the text file.
- Requirements:
- - Python 3.x
- Usage:
- 1. Save this script as 'csv2txt.py'.
- 2. Ensure your CSV file ('example.csv') is in the same directory as the script.
- 3. Run the script.
- 4. The converted text file ('csv2txt.txt') will be generated in the same directory.
- Note: Adjust the 'csv_filename' and 'txt_filename' variables in the script as needed.
- """
- import csv
- def csv_to_txt(csv_filename, txt_filename):
- with open(csv_filename, 'r') as csvfile, open(txt_filename, 'w') as txtfile:
- csvreader = csv.reader(csvfile)
- for row in csvreader:
- # Assuming each row in the CSV file is a separate line in the text file
- txtfile.write(','.join(row) + '\n')
- if __name__ == "__main__":
- csv_filename = 'example.csv'
- txt_filename = 'csv2txt.txt'
- csv_to_txt(csv_filename, txt_filename)
- print(f"Converted '{csv_filename}' to '{txt_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement