3

This is an example of my CSV file:

04/Feb/2016:06:38:44-0500,ab,3,10,57,200,10254
04/Feb/2016:06:39:07-0500,cd,1,42,168,304,0
04/Feb/2016:06:39:07-0500,ef,1,43,169,304,0
04/Feb/2016:06:39:07-0500,ab,1,43,170,304,0
04/Feb/2016:06:39:07-0500,cd,1,44,171,304,0
04/Feb/2016:06:39:07-0500,ef,1,45,172,304,0

I would like to fetch string in 2nd column, create file called like that string if file doesnt already exist and add that particular line in a file. So something like this:

fetch string in 2nd column -> "ab" -> if file doesnt exist create file called "ab.csv" -> open file and add line "04/Feb/2016:06:38:44-0500,ab,3,10,57,200,10254"
fetch string in 2nd column -> "cd" -> if file doesnt exist create file called "cd.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,cd,1,42,168,304,0"
fetch string in 2nd column -> "ef" -> if file doesnt exist create file called "ef.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,ef,1,43,169,304,0"
fetch string in 2nd column -> "ab" -> if file doesnt exist create file called "ab.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,ab,1,43,170,304,0"
fetch string in 2nd column -> "cd" -> if file doesnt exist create file called "cd.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,cd,1,44,171,304,0"
fetch string in 2nd column -> "ef" -> if file doesnt exist create file called "ef.csv" -> open file and add line "04/Feb/2016:06:39:07-0500,ef,1,45,172,304,0"

Result:

ab.csv:
04/Feb/2016:06:38:44-0500,ab,3,10,57,200,10254
04/Feb/2016:06:39:07-0500,ab,1,43,170,304,0
----------------------------------------------
cd.csv:
04/Feb/2016:06:39:07-0500,cd,1,42,168,304,0
04/Feb/2016:06:39:07-0500,cd,1,44,171,304,0
----------------------------------------------
ef.csv:
04/Feb/2016:06:39:07-0500,ef,1,43,169,304,0
04/Feb/2016:06:39:07-0500,ef,1,45,172,304,0

Any help appriciated!

2 Answers 2

5
+100

Using awk

$ awk -F, '{print >> $2".csv"}' file.csv

$ cat ab.csv
04/Feb/2016:06:38:44-0500,ab,3,10,57,200,10254
04/Feb/2016:06:39:07-0500,ab,1,43,170,304,0
$ cat cd.csv
04/Feb/2016:06:39:07-0500,cd,1,42,168,304,0
04/Feb/2016:06:39:07-0500,cd,1,44,171,304,0
$ cat ef.csv
04/Feb/2016:06:39:07-0500,ef,1,43,169,304,0
04/Feb/2016:06:39:07-0500,ef,1,45,172,304,0
$

Do bear in mind though that real CSV files might include quoted commas within their comma separated fields - so a proper CSV parser is always recommended for serious use: see for example How to read a CSV file using Perl? or PyMOTW: Comma-separated value files.

1
  • This is great and it works for me and for now! I thought that solution to the problem is much more complicated. Thank you very much! Oct 7, 2016 at 15:13
2

I don't know if you are still looking for pythonic solution. I'm amazed by the simplicity in steeldriver's answer, didn't realised awk is so powerful.

#!/usr/bin/env python

import csv
import os

def main():
    with open("file.csv", "rb") as f:
        reader = csv.reader(f)
        for row in reader:
            fname = row[1] + ".csv"
            with open(fname, 'w') as f:
                f.write(','.join([i for i in row]))


main()

Can't blame me for trying :D for the shiny bounty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.