Created
February 26, 2018 13:30
-
-
Save xaota/52b25263fd9a5b99c643f2a20227499c to your computer and use it in GitHub Desktop.
конвертация построчного json в csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rdmd | |
import std.stdio, std.string, std.json, std.array, std.conv; | |
import std.algorithm: each, map, sum, fold; | |
import std.algorithm.comparison: equal; | |
import std.array: join; | |
import std.exception: enforce; | |
void main() { | |
jsonTxt2csv("input.json.txt", "output.csv") | |
.writeHeader() | |
.process(); | |
} | |
struct jsonTxt2csv { | |
public: | |
this(string input, string output) { | |
this(File(input), File(output, "w")); | |
} | |
this(File input, File output) { | |
this.lines = file2range(input); | |
this.output = output; | |
this.header = lines.front.object().keys; | |
} | |
ref jsonTxt2csv writeHeader() { | |
this.output.writeln(join(this.header ~ "summ", ';')); | |
return this; | |
} | |
void process() { | |
lines.each!(line => this.processLine(line)); | |
} | |
private: | |
alias jsonLines = typeof(file2range(File.init)); | |
jsonLines lines; | |
File output; | |
string[] header; | |
void processLine(JSONValue json) { | |
string[] lineKeys = json.object().keys; | |
enforce(equal(this.header, lineKeys), "Error"); // Если ключи не совпадают - ошибка | |
this.writeLine(json); // запись строки в CSV | |
} | |
void writeLine(JSONValue json) { | |
double[] items = this.header | |
.map!(key => json[key].floating) | |
.map!(e => e ^^ 2) | |
.array; | |
items ~= items.sum; | |
this.output.writeln(items.to!(string[]).join(";")); | |
} | |
auto file2range(File input) { | |
return input | |
.byLine() | |
.map!(s => parseJSON(s, JSONOptions.specialFloatLiterals)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment