【发布时间】:2019-03-17 04:11:16
【问题描述】:
我想获取文件中特定格式的数字列表。但我没有得到数字列表的任何格式(如 %s %d)。
我的文件包含如下文本:
[1;2] [2] 5
[45;37] [9] 33
[3] [2;4] 1000
我尝试了以下
value split_input str fmt = Scanf.sscanf str fmt (fun x y z -> (x,y,z));
value rec read_file chin acc fmt =
try let line = input_line chin in
let (a,b,c) = split_input line fmt in
let acc = List.append acc [(a,b,c)] in
read_file chin acc fmt
with
[ End_of_file -> do { close_in chin; acc}
];
value read_list =
let chin = open_in "filepath/filename" in
read_file chin [] "%s %s %d";
问题在于最后指定的格式。我使用相同的代码从其他文件中获取数据,其中数据的格式为 (string * string * int)。
要重用相同的代码,我必须以字符串形式接收上述文本,然后根据我的要求进行拆分。我的问题是:整数列表是否有像 %s %d 这样的格式,以便我直接从文件中获取列表,而不是编写另一个代码将字符串转换为列表。
【问题讨论】:
标签: ocaml