-
Notifications
You must be signed in to change notification settings - Fork 51
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
Labels
feature request
Discussion about a new feature
Comments
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;
}
} |
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
Is there an existing issue for this?
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
The text was updated successfully, but these errors were encountered: