目前我下面的代码调用JsonToCsv((来解析JSON文件并相应地附加到csv,因此结果如下:
result.csv
File Name Page Practice Name
file1.json 1 Associates & Co
然而,正如您所看到的,我使用for循环来迭代一堆JSON文件,我的意图是它解析它们并将它们附加到csv中。因此,如果我有3个json文件,那么预期的csv文件应该是这样的:
File Name Page Practice Name
fileXYZ.json 1 XYZ & Co
fileAB2.json 1 ABC & Co
file1.json 1 Associates & Co
然而,发生的事情是csv文件被覆盖,我只看到csv文件中最后一个文件的结果。如何将其附加到同一个文件?
static void Main(string[] args)
{
//Output to CSV
foreach (var jsonFile in Directory.GetFiles(jsonFilesPath))
{
JsonToCsv(jsonFile, csvFilePath);
}
}
public static void JsonToCsv(string jsonInputFile, string csvFile)
{
using (var p = new ChoJSONReader(jsonInputFile).WithJSONPath("$..readResults"))
{
using (var w = new ChoCSVWriter(csvFile)//.WithFirstLineHeader())
.WithField("FileName", fieldName: "File Name")
.WithField("Page")
.WithField("PracticeName", fieldName: "Practice Name")
.WithFirstLineHeader()
)
{
// Limit the result to page 1 since the fields below only exist on the 1st page
w.Write(p
.Where(r1 => r1.page == 1)
.Select(r1 =>
{
var lines = (dynamic[])r1.lines;
return new
{
FileName = inputFile,
Page = r1.page,
PracticeName = lines[6].text,
};
}
));
}
}
}
第一个选项。我建议您更改方法签名以接受多个文件。
public static void JsonToCsv(IEnumerable<string> jsonInputFiles, string csvFile)
{
using (var w = new ChoCSVWriter(csvFile)
.WithField("FileName", fieldName: "File Name")
.WithField("Page")
.WithField("PracticeName", fieldName: "Practice Name")
.WithFirstLineHeader()
)
{
foreach (var jsonInputFile in jsonInputFiles)
{
using (var p = new ChoJSONReader(jsonInputFile).WithJSONPath("$..readResults"))
{
w.Write(p
.Where(r1 => r1.page == 1)
.Select(r1 =>
{
var lines = (dynamic[])r1.lines;
return new
{
FileName = inputFile,
Page = r1.page,
PracticeName = lines[6].text,
};
}
));
}
}
}
}
第二个选项。使用FileStream和Append选项,并执行一些额外的代码来处理不同的情况,请参阅注释。
using (var fs = new FileStream(@"D:file.csv", FileMode.Append, FileAccess.Write))
{
using (var writer = new ChoCSVWriter(fs))
{
writer.WithField("FileName", fieldName: "File Name")
.WithField("Page")
.WithField("PracticeName", fieldName: "Practice Name");
if (fs.Position == 0) // we don't need header if file already existed before
{
writer.WithFirstLineHeader();
}
writer.Write(new
{
FileName = "Test",
Page = 1,
PracticeName = "Abc"
});
}
fs.Write(Environment.NewLine); // append new line carrier so we don't write to the same line when file reopened for writing
}
第三个选项-来自评论(基于第二个(。在创建和附加选项之间切换
bool someCondition = true;
FileMode fileMode = someCondition ? FileMode.Append : FileMode.Create;
using (var fs = new FileStream(@"D:file.csv", fileMode, FileAccess.Write))