Created
August 1, 2021 02:42
-
-
Save anuradhawick/aeacced0d38a1ad44168ad526d684696 to your computer and use it in GitHub Desktop.
Gather CSV file(s) into XLSX Sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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