将文本文件解析为变量列表?
我有一个文本文件(当前为 CSV 格式),如下所示:
RACE,"race_human"
GENDER,"male"
AGE,30
ALIGNMENT,"align_lawful_good"
DEITY,"Old Faith"
但是,我想将文本文件解释为变量列表。即:
var RACE:string = "race_human";
有没有一种简单的方法可以做到这一点,例如用程序代码使用的本地语言重新格式化文本文件?
I have a text file (currently in CSV format) as follows:
RACE,"race_human"
GENDER,"male"
AGE,30
ALIGNMENT,"align_lawful_good"
DEITY,"Old Faith"
However, I want to interpret the text file as if it were a list of variables. I.e.:
var RACE:string = "race_human";
Is there an easy way to do this, for instance reformatting the text file in the native language used by the program code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以用逗号分割每一行,然后将每个项目放入字典中(前提是键是唯一的)。如果某个键可能不存在,请使用 Dictionary.TryGetValue。
编辑:您可能对使用 CSV 文件的 FileHelpers 感兴趣。
You could split each line on the comma then place each item into a dictionary (provided the keys are unique). Use Dictionary.TryGetValue if there's a chance that a key does not exist.
EDIT: you might be interested in FileHelpers to work with CSV files.