Skip to content

Instantly share code, notes, and snippets.

@anuradhawick
Created August 1, 2021 02:42
Show Gist options
  • Save anuradhawick/aeacced0d38a1ad44168ad526d684696 to your computer and use it in GitHub Desktop.
Save anuradhawick/aeacced0d38a1ad44168ad526d684696 to your computer and use it in GitHub Desktop.
Gather CSV file(s) into XLSX Sheets
import csv
import pandas as pd
from collections import defaultdict
header = None
created_sheets = defaultdict(list)
lc = None
with open('INPUT.csv') as f:
reader = csv.reader(f)
lines = list(reader)
lc = len(lines) - 1
header = lines[0][1:]
for line in lines[1:]:
sheet = line[0]
if sheet not in created_sheets:
created_sheets[sheet].append(header)
created_sheets[sheet].append(line[1:])
writer = pd.ExcelWriter('OUTPUT.xlsx')
lco = 0
for key, data in created_sheets.items():
df = pd.DataFrame(data)
df.to_excel(writer, key, index=False, header=False)
lco += len(data) - 1
assert(lc==lco)
writer.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment