Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] CSV #646

Open
1 task done
Jason2605 opened this issue Aug 29, 2023 · 2 comments
Open
1 task done

[FEATURE] CSV #646

Jason2605 opened this issue Aug 29, 2023 · 2 comments
Labels
feature request Discussion about a new feature

Comments

@Jason2605
Copy link
Member

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem?

Add the ability to parse CSVs

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

No response

@Jason2605 Jason2605 added the feature request Discussion about a new feature label Aug 29, 2023
@briandowns
Copy link
Contributor

Do you want this in Dictu or C? If Dictu, I have:

// Module csv provides functions to read and write data in CSV format.

// Opts requires 1 argument, the path of the file to be parsed. 2 other
// optional arguments are available. firstLineHeader (default: true) indicates
// whether or not the first line of the file is a header, and delimiter 
// (default: ",") which indicates the record delimiter.
class Opts {
    init(var path,
        var firstLineHeader=false,
        var delimiter=",",
        var commentCharacter="#",
        var startLine = 0) {}
}

// Parser opens the file in the given path, parses each line and record, and
// saves them to a list and returns it.
class Parser {
    private columnCount;

    init(var opts) {
        this.opts = opts;
    }

    parse() {
        if (this.opts.delimiter == this.opts.commentCharacter) {
            return Error("delimiter and comment character aren't allowed to be the same");
        }

        var data = [];

        with(this.opts.path, "r") {
            var line;
            var lineNum = 0;

            while((line = file.readLine()) != nil) {
                lineNum += 1;

                if (lineNum == 1 and this.opts.firstLineHeader) {
                    continue;
                }

                if (line.startsWith(this.opts.commentCharacter)) {
                    continue;
                }

                var record = line.split(this.opts.delimiter);
                this.columnCount = record.len();

                for (var i = this.opts.startLine; i < record.len(); i += 1) {
                    record[i] = record[i].strip();
                }

                data.push(record);
            }
        }

        return Success(data);
    }

    // write
    write(data) {
        if (data == "") {
            return Error("given data empty");
        }

        with(this.opts.path, "w") {

        }

        return Success(nil);
    }

    // writeAll
    writeAll(data) {
        if (data.len() == 0) {
            return Error("given data empty");
        }

        with(this.opts.path, "w") {

        }

        return Success(nil);
    }

    getColumnCount() {
        return this.columnCount;
    }
}

@Jason2605
Copy link
Member Author

Oh nice!! I wouldn’t be against to the parser for this being in Dictu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Discussion about a new feature
Projects
None yet
Development

No branches or pull requests

2 participants