[text] A

Viewer

  1. import csv
  2.  
  3. def save_to_csv(entries, filename):
  4.     with open(filename, "w", newline="") as file:
  5.         writer = csv.writer(file)
  6.         writer.writerows(entries)
  7.  
  8. def main():
  9.     # Your existing code to retrieve and print the data goes here...
  10.  
  11.     # Assuming 'csvfile' contains the CSV data as a string
  12.     csvfile = """
  13.     Branch,Line,Line_Description,Registered_Sales,Unit_Sales,Explained_wastage,Explained_wastage_Quality,Reductions_Sterl
  14.     101 Bishop's Stortford,2105,WR Ess Rich Tea Finger Biscuits 250g,£19.55,23,£0.00,£0.00,£0.00
  15.     101 Bishop's Stortford,2124,WR Ess Cream Crackers 300g,£28.00,40,£0.00,£0.00,£0.00
  16.     101 Bishop's Stortford,2141,WR Ess Garibaldi Biscuits 200g,£22.95,17,£0.00,£0.00,£0.00
  17.     101 Bishop's Stortford,2144,WR Ess Shortcake Biscuits 400g,£15.00,12,£0.00,£0.00,£0.00
  18.     """
  19.  
  20.     # Extract entries
  21.     entries = [line.split(",") for line in csvfile.strip().split("\n")]
  22.  
  23.     # Save to CSV with custom filename
  24.     custom_filename = "custom_sales_data.csv"
  25.     save_to_csv(entries, custom_filename)
  26.     print(f"Data saved to {custom_filename}")
  27.  
  28. if __name__ == "__main__":
  29.     main()

Editor

You can edit this paste and save as new:


File Description
  • A
  • Paste Code
  • 07 May-2024
  • 1.15 Kb
You can Share it: