54 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import os
 | |
| import csv
 | |
| from datetime import datetime
 | |
| 
 | |
| # Cesty k souborům
 | |
| source_file = "finance.csv"
 | |
| archive_folder = "2024"
 | |
| current_year = datetime.now().year  # Aktuální rok
 | |
| 
 | |
| # Vytvoření složky pro archiv
 | |
| if not os.path.exists(archive_folder):
 | |
|     os.makedirs(archive_folder)
 | |
| 
 | |
| # Načtení dat
 | |
| rows_current_year = []
 | |
| rows_by_year = {}
 | |
| 
 | |
| with open(source_file, newline='', encoding='utf-8') as csvfile:
 | |
|     reader = csv.reader(csvfile)
 | |
|     header = next(reader)  # Načtení hlavičky CSV
 | |
| 
 | |
|     for row in reader:
 | |
|         if len(row) < 5:
 | |
|             continue  # Přeskočení nekompletních řádků
 | |
| 
 | |
|         try:
 | |
|             year = datetime.strptime(row[0], "%Y-%m-%d").year  # Datum v prvním sloupci
 | |
|         except ValueError:
 | |
|             continue  # Neplatné datum
 | |
| 
 | |
|         if year == current_year:
 | |
|             rows_current_year.append(row)  # Necháme v hlavním souboru
 | |
|         else:
 | |
|             if year not in rows_by_year:
 | |
|                 rows_by_year[year] = []
 | |
|             rows_by_year[year].append(row)
 | |
| 
 | |
| # Uložení archivovaných souborů
 | |
| for year, rows in rows_by_year.items():
 | |
|     file_path = os.path.join(archive_folder, f"finance_{year}.csv")  # Správný název
 | |
|     with open(file_path, "w", newline='', encoding='utf-8') as csvfile:
 | |
|         writer = csv.writer(csvfile)
 | |
|         writer.writerow(header)  # Zápis hlavičky
 | |
|         writer.writerows(rows)
 | |
| 
 | |
| # Uložení zpět do finance.csv (pouze aktuální rok)
 | |
| with open(source_file, "w", newline='', encoding='utf-8') as csvfile:
 | |
|     writer = csv.writer(csvfile)
 | |
|     writer.writerow(header)
 | |
|     writer.writerows(rows_current_year)
 | |
| 
 | |
| print(f"Archivace dokončena. Staré záznamy přesunuty do {archive_folder}/finance_YYYY.csv.")
 | |
| print(f"Finance.csv nyní obsahuje pouze data z roku {current_year}.")
 |