Alukard_X
Grand Guru
- Joined
- Oct 3, 2020
- Messages
- 795

Python:
import os
import zipfile
from datetime import datetime
import pandas as pd
# Folder containing ZIP files
folder_path = r'/home/user/files'
# List of CSV filenames inside ZIPs
csv_files = [
'Propellerads_desktop_mac.csv',
'Propellerads_desktop_windows.csv',
'Propellerads_mobile_android.csv',
'Propellerads_mobile_ios.csv'
]
# List of columns to remove from final DataFrame
columns_to_drop = ['B1', 'B2', 'B3', 'B4', 'B5']
# Initialize an empty list to collect dataframes
all_data = []
# List all ZIP files in the folder
zip_files = [f for f in os.listdir(folder_path) if f.endswith('.zip')]
for zip_filename in zip_files:
zip_path = os.path.join(folder_path, zip_filename)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
for csv_name in csv_files:
try:
info = zip_ref.getinfo(csv_name)
# Extract date from info.date_time tuple
csv_date = datetime(*info.date_time).date()
except KeyError:
# CSV file not found in ZIP
continue # Skip if not present
# Read CSV data from ZIP with flexible delimiter
df = None
for delimiter in [',', ';']:
try:
with zip_ref.open(csv_name) as file:
df_candidate = pd.read_csv(file, delimiter=delimiter)
# Check for required columns
if 'COUNTRY_CODE' in df_candidate.columns and 'TRAFFIC' in df_candidate.columns:
df = df_candidate
break # Successfully read with this delimiter
except Exception as e:
# Could log error here if needed
continue
if df is not None:
# Assign date from file modification timestamp
df['Date'] = pd.to_datetime(csv_date)