如果您指定类型,您的玩具示例会给您错误,因为那里有一些缺失值.这些缺失值是因为字符/在朱莉娅字符串,这样你的表的类型将结束是手柄Any为readdlm无法弄清楚这些是否为数字/字符值.Row1只有1个值,而row2有2个,等等,给你缺少的值.
如果您的所有数据在文本文件中都很好且干净,您可以在以下位置设置表的类型readdlm:
int_table = readdlm("FileName2.txt", Int16)
int_table
3x3 Array{Int16,2}:
7 0 0
31 16 0
90 2 53
Run Code Online (Sandbox Code Playgroud)
在哪里FileName2.txt:
7 0 0
31 16 0
90 2 53
Run Code Online (Sandbox Code Playgroud)
但是,如果您的数据缺少值,则需要将它们转换为某些数值或使用DataFrames包来处理它们.我假设你想要一个纯整数数组,所以我用0填充值:
any_table = readdlm("FileName.txt")
any_table
3x3 Array{Any,2}:
7 "" ""
31 16 ""
90 2 53
# fill missing values with 0
any_table[any_table .== ""] = 0
# convert to integer table
clean_array = Array{Int16}(any_table)
clean_array
3x3 Array{Int16,2}:
7 0 0
31 16 0
90 2 53
Run Code Online (Sandbox Code Playgroud)
Readdlm为您关闭文件,因此您不必担心.
| 归档时间: |
|
| 查看次数: |
1111 次 |
| 最近记录: |