Split CSV file into multiple files sorted my colum

Question:

Hi im a Python noob and i want to make a programm to split my csv file into multiple csv files

the file looks something like this:

main.csv

Name;Lieferung;Name;Ort;Postleitzahl
somename;60072470;somename;someadress;83620
somename;60071938;somename;someadress;48691
somename;60072194;somename;someadress;13595
somename;60072194;somename;someadress;13595
somename;60072511;somename;someadress;82140

and i want the code to automaticly create multiple csv files grouped by Lieferung:

60072470.csv

somename;60072470;somename;someadress;83620

60071938.csv

somename;60071938;somename;someadress;48691

60072194.csv

somename;60072194;somename;someadress;13595
somename;60072194;somename;someadress;13595

60072511.csv

somename;60072511;somename;someadress;82140

Can someone help me, i did a lot o reaserch but i cant make it 🙁

THX a Lot

Asked By: Felix

||

Answers:

Try this:

for Lieferung in df.Lieferung.unique():
    df[df.Lieferung == Lieferung].to_csv(str(Lieferung)+'.csv')

You first iterate over the unique Lieferung values (print the output of df.Lieferung.unique() to see what it does ) and then use these to select only the relevant subset of data (df[df.Lieferung == Lieferung]) that you can export to csv using Lieferung to create the filename.

Answered By: Sheldon

Pandas can do that. After installing you just do something like this:

import pandas as pd

df = pd.read_csv("main.csv", sep=";")
for index, group in df.groupby("Lieferung"):
    group.reset_index(drop=False).to_csv(f"{index}.csv", sep=";", index=False)
Answered By: qmeeus
import csv

data = {}

with open("input.csv", newline="") as file:
    reader = csv.reader(file, delimiter=";")
    header = next(reader)
    for row in reader:
        data.setdefault(row[1], [])
        data[row[1]].append(row)

for name, row in data.items():
    with open(f"{name}.csv", "w", newline="") as file:
        writer = csv.writer(file, delimiter=";")
        writer.writerow(header)
        writer.writerows(row)
Answered By: ali omar
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.