Advertisement
Python253

csv2txt

Mar 13th, 2024
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: csv2txt.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script converts a CSV file (.csv) to a text file (.txt).
  10. It reads the CSV content and writes each row as a separate line in the text file.
  11.  
  12. Requirements:
  13. - Python 3.x
  14.  
  15. Usage:
  16. 1. Save this script as 'csv2txt.py'.
  17. 2. Ensure your CSV file ('example.csv') is in the same directory as the script.
  18. 3. Run the script.
  19. 4. The converted text file ('csv2txt.txt') will be generated in the same directory.
  20.  
  21. Note: Adjust the 'csv_filename' and 'txt_filename' variables in the script as needed.
  22. """
  23. import csv
  24.  
  25. def csv_to_txt(csv_filename, txt_filename):
  26.     with open(csv_filename, 'r') as csvfile, open(txt_filename, 'w') as txtfile:
  27.         csvreader = csv.reader(csvfile)
  28.         for row in csvreader:
  29.             # Assuming each row in the CSV file is a separate line in the text file
  30.             txtfile.write(','.join(row) + '\n')
  31.  
  32. if __name__ == "__main__":
  33.     csv_filename = 'example.csv'
  34.     txt_filename = 'csv2txt.txt'
  35.     csv_to_txt(csv_filename, txt_filename)
  36.     print(f"Converted '{csv_filename}' to '{txt_filename}'.")
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement