Advertisement
Python253

create_example_csv

Mar 11th, 2024
537
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: create_example_csv.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9. This script creates an example CSV file ('example.csv') with sample data.
  10.  
  11. Requirements:
  12. - Python 3.x
  13.  
  14. Usage:
  15. 1. Save this script as 'create_example_csv.py'.
  16. 2. Run the script.
  17.  
  18. Note: Adjust the 'example_filename' variable in the script as needed.
  19. """
  20. import csv
  21.  
  22. def create_example_csv(filename):
  23.     # Define the data to be written to the CSV file
  24.     data = [
  25.         ['Name', 'Age', 'City'],
  26.         ['John Doe', 25, 'New York'],
  27.         ['Jane Smith', 30, 'San Francisco'],
  28.         ['Bob Johnson', 22, 'Los Angeles']
  29.     ]
  30.  
  31.     # Write data to the CSV file
  32.     with open(filename, 'w', newline='') as csvfile:
  33.         csvwriter = csv.writer(csvfile)
  34.         csvwriter.writerows(data)
  35.  
  36. if __name__ == "__main__":
  37.     # Set the filename for the example CSV file
  38.     example_filename = 'example.csv'
  39.  
  40.     # Create the example CSV file in the current working directory
  41.     create_example_csv(example_filename)
  42.  
  43.     print(f"Example CSV file '{example_filename}' created in the current working directory.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement