nested csv into tsv

bioinfornatics bioinfornatics at fedoraproject.org
Sun Mar 18 09:19:29 PDT 2012


Le dimanche 18 mars 2012 à 16:53 +0100, Jesse Phillips a écrit :
> On Sunday, 18 March 2012 at 14:45:42 UTC, bioinfornatics wrote:
> > ________________________________
> > $ ./test_csv
> > std.csv.CSVException@/usr/include/d/std/csv.d(1047): Can't 
> > parse string:
> > "[" is missing
> > std.conv.ConvException@/usr/include/d/std/conv.d(2714): Can't 
> > parse
> > string: "[" is missing
> > std.conv.ConvException@/usr/include/d/std/conv.d(1597): 
> > Unexpected 'd'
> > when converting from type string to type string[]
> > ________________________________
> 
> I'm going to harbor a guess that you have confused std.conv.to by 
> using two different types for field3
> 
>     @property void field3( string field ){
>         _field3 = field.split(";");
>     }
>     @property string[] field3(  ){
>         return _field3;
>     }
> 
> The first says it is a string, the second a string[]. I assume 
> that std.conv.to sees field3 as a string[] and is trying to 
> convert a string to it. In this case it expects the string to be 
> formatted, ["this is an","array","of string"]

If i do this:
________________________________
>import std.csv;
import std.string;
import std.stdio;

struct Data{
    public:
        string field1;
        string field2;

    @property void field3( string field ){
        _field3 = field.split(";");
    }

    @property string field3( ){
        string result;
        foreach( item; _field3 )
            result ~= " %s;".format( item );
        return result;
    }

    @property attributes(){
        return  _field3;
    }

    private:
        string[] _field3;
}

void main(){
    Data[] result;
    File f = File( "data.csv", "r" );
    foreach( char[] line; f.byLine() ){
        result ~= csvReader!Data(line, '\t').front;
    }
}
________________________________


Same result



More information about the Digitalmars-d-learn mailing list