简体   繁体   中英

How to split one csv into multiple files in python

I have a csv file (world.csv) looks like this :

"city","city_alt","lat","lng","country"
"Mjekić","42.6781","20.9728","Kosovo"
"Mjekiff","42.6781","20.9728","Kosovo"
"paris","42.6781","10.9728","France"
"Bordeau","16.6781","52.9728","France"
"Menes","02.6781","50.9728","Morocco"
"Fess","6.6781","3.9728","Morocco"
"Tanger","8.6781","5.9728","Morocco"

And i want to split it to multiple file by country like this:

Kosovo.csv :

"city","city_alt","lat","lng","country"
"Mjekić","42.6781","20.9728","Kosovo"
"Mjekiff","42.6781","20.9728","Kosovo"

France.csv :

"city","city_alt","lat","lng","country"
"paris","42.6781","10.9728","France"
"Bordeau","16.6781","52.9728","France"

Morroco.csv :

"city","city_alt","lat","lng","country"
"Menes","02.6781","50.9728","Morocco"
"Fess","6.6781","3.9728","Morocco"
"Tanger","8.6781","5.9728","Morocco"

try this:

filter the columns based on the country name. Then convert that to csv file using to_csv in pandas

df = pd.read_csv('test.csv')

france = df[df['country']=='France']
kosovo = df[df['country']=='Kosovo']
morocco = df[df['country']=='Morocco']

france.to_csv('france.csv', index=False)
kosovo.to_csv('kosovo.csv', index=False)
morocco.to_csv('morocco.csv', index=False)

If you can't use pandas you can use the built-in csv module and itertools.groupby() function. You can use this to group by country.

from itertools import groupby
import csv

with open('world.csv') as csv_file:
    reader = csv.reader(csv_file)
    next(reader) #skip header
    
    #Group by column (country)
    lst = sorted(reader, key=lambda x : x[4])
    groups = groupby(lst, key=lambda x : x[4])

    #Write file for each country
    for k,g in groups:
        filename = k + '.csv'
        with open(filename, 'w', newline='') as fout:
            csv_output = csv.writer(fout)
            csv_output.writerow(["city","city_alt","lat","lng","country"])  #header
            for line in g:
                csv_output.writerow(line)

The easiest way to do this is as below: #create a folder called "adata" for example in your working directory #import glob

for i,g in df.groupby('CITY'):
    g.to_csv('adata\{}.csv'.format(i), header=True, index_label='Index')
print(glob.glob('adata\*.csv'))
filenames = sorted(glob.glob('adata\*.csv'))

for f in filenames:
    #your intended processes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM