csv file management for appending values.

I have a software which extracts following value in a csv format on a daily basis.

NAME,TYPE,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,DATE,ITEM9,ITEM10
A,TYPE 1,124,123,190,142,124,175,199,130,19-Jul-19,136,PEN
B,TYPE 2,170,153,150,132,134,615,149,140,19-Jul-19,156,PENCIL
C,TYPE 3,170,173,170,136,138,175,189,170,19-Jul-19,186,ERASER

I manually used to append this data in files A.csv , B.csv, and C.csv on a daily basis.

I wish to develop a code to identify the NAME and paste the entire line to the respective Name file.

For Reference I am pasting the values already present in files A.csv , B.csv, and C.csv

File A.csv
NAME,TYPE,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,DATE,ITEM9,ITEM10
A,TYPE 1,108,129,112,120,131,145,118,148,11-Jul-19,116,PEN
A,TYPE 1,100,137,143,133,110,111,146,137,12-Jul-19,112,PEN
A,TYPE 1,144,147,123,107,115,118,130,117,13-Jul-19,131,PEN
A,TYPE 1,119,116,109,124,148,123,149,108,14-Jul-19,144,PEN
A,TYPE 1,100,101,142,127,121,136,100,110,15-Jul-19,135,PEN
A,TYPE 1,145,101,111,101,109,103,133,110,16-Jul-19,149,PEN
A,TYPE 1,111,100,104,115,128,119,118,147,17-Jul-19,107,PEN
A,TYPE 1,128,116,150,102,144,132,115,150,18-Jul-19,101,PEN

File B.csv
NAME,TYPE,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,DATE,ITEM9,ITEM10
B,TYPE 2,120,143,150,132,134,115,149,150,11-Jul-19,136,PENCIL
B,TYPE 2,108,119,118,118,101,112,121,120,12-Jul-19,125,PENCIL
B,TYPE 2,112,130,121,132,117,148,137,138,13-Jul-19,124,PENCIL
B,TYPE 2,128,133,106,141,131,110,132,114,14-Jul-19,144,PENCIL
B,TYPE 2,127,134,130,117,108,133,114,126,15-Jul-19,129,PENCIL
B,TYPE 2,116,119,126,114,133,115,144,113,16-Jul-19,121,PENCIL
B,TYPE 2,107,128,105,100,107,139,141,122,17-Jul-19,147,PENCIL
B,TYPE 2,138,136,143,118,146,101,141,121,18-Jul-19,123,PENCIL

File C.csv
NAME,TYPE,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,DATE,ITEM9,ITEM10
C,TYPE 3,155,136,149,116,130,135,132,133,11-Jul-19,107,ERASER
C,TYPE 3,130,134,135,128,136,146,135,155,12-Jul-19,155,ERASER
C,TYPE 3,151,133,134,116,129,155,121,105,13-Jul-19,109,ERASER
C,TYPE 3,106,149,141,137,112,130,100,140,14-Jul-19,115,ERASER
C,TYPE 3,113,140,125,142,121,121,134,103,15-Jul-19,152,ERASER
C,TYPE 3,103,104,131,133,139,125,122,114,16-Jul-19,111,ERASER
C,TYPE 3,104,121,124,147,101,103,116,135,17-Jul-19,135,ERASER
C,TYPE 3,137,101,112,147,152,122,146,124,18-Jul-19,136,ERASER

Please advice me what could be a potential solution for the same. Its a bit urgent so if feasible a model code would be extremely helpful as I am new to programming.

Thanking in anticipation.
There you go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// csv.d

void main() {

    import std;


    File[string] outputFiles;

    auto inputFile = File("input.csv");

    auto lines = inputFile.byLine;

    lines.popFront; // skip header


    foreach (line; lines) {

        const name = line.splitter(',').front;

        // open output file if not already open in append mode 

        if (name !in outputFiles) { 

            outputFiles[name] = File(name ~ ".csv", "a");
        }

        outputFiles[name].writeln(line);
    }
}

This assumes that your output csv files end with a newline.
Grab DMD from here https://dlang.org/download.html and
run the code with: rdmd csv.d
Last edited on
or, similarly,
make an array of output files and open them all.
then read the main file line by line.
write the line you read to array[first_letter - 'A']
close all files. done.

roughly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream infile(filename);
ofstream outfiles[3];
outfiles[0].open("A.csv", ios::app);
outfiles[1].open("B.csv", ios::app);
outfiles[2].open("C.csv", ios::app);
string s;
while(getline(infile,s))
{
   outfiles[(int)(s[0]-'A')] << s << endl; //I am assuming the first letter is A/B/C
}
outfiles[0].close();
outfiles[1].close();
outfiles[2].close();

Last edited on
Topic archived. No new replies allowed.