[英]How can I render a TSV file into JSON when it is saved as .txt?
I have a very large table saved as a text file that is imported to my company from an external source.我有一个非常大的表格保存为从外部来源导入我公司的文本文件。 The formatting in this file looks like it should be a.TSV.
此文件中的格式看起来应该是.TSV。 I need to find a way to get it into an Observable Collection.
我需要找到一种方法将其放入 Observable Collection。 My thought was that I could use a Deserializer to automatically parse it out.
我的想法是我可以使用反序列化器来自动解析它。 That didnt work at all since I dont have any quotes saved in the whole document.
这根本不起作用,因为我没有在整个文档中保存任何引号。 Then I found this .
然后我发现了这个。 The answer from giotskhada, seems like it would work, but its in Python, and I dont have a median file to save it into.
来自 giotskhada 的答案似乎可行,但它在 Python 中,我没有中间文件可以保存它。 Here is an example of the txt file:
下面是一个txt文件的例子:
id FieldName1 FieldName2 FieldName3 FieldName4
1 test1 test3 test4
2 test2 test3 test4
3 test1 test2 test3 test4
and this is how I would like it to read out:这就是我希望它读出的方式:
[ {"id":"1",
"FieldName1":"test1",
"FieldName2":"null",
"FieldName3":"test3",
"FieldName4":"test4"}
},
{"id":"2",
"FieldName1":"null",
"FieldName2":"test2",
"FieldName3":"test3",
"FieldName4":"test4"}
},
{"id":"3",
"FieldName1":"test1",
"FieldName2":"test2",
"FieldName3":"test3",
"FieldName4":"test4"}
]
What can I do to make this happen in C#?我该怎么做才能在 C# 中发生这种情况?
Well, if you open to use external library, Cinchoo ETL is one will help to achieve to handle large file in expected format.好吧,如果您打开使用外部库, Cinchoo ETL是一种有助于实现以预期格式处理大文件的方法。
install-package ChoETL.JSON
安装包 ChoETL.JSON
Here is sample working code这是示例工作代码
string tsv = @"id FieldName1 FieldName2 FieldName3 FieldName4
1 test1 test3 test4
2 test2 test3 test4
3 test1 test2 test3 test4";
StringBuilder json = new StringBuilder();
using (var r = ChoTSVReader.LoadText(tsv)
.WithFirstLineHeader()
)
{
using (var w = new ChoJSONWriter(json))
w.Write(r);
}
Console.WriteLine(json.ToString());
Output: Output:
[
{
"id": "1",
"FieldName1": "test1",
"FieldName2": null,
"FieldName3": "test3",
"FieldName4": "test4"
},
{
"id": "2",
"FieldName1": null,
"FieldName2": "test2",
"FieldName3": "test3",
"FieldName4": "test4"
},
{
"id": "3",
"FieldName1": "test1",
"FieldName2": "test2",
"FieldName3": "test3",
"FieldName4": "test4"
}
]
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.