julia读取含有字符和数字的文本数据

# 假设有如下的文本文件dat.txt
a, 1.0
b, 2.0
c, 3.0
d, 4.0
e, 5.0
f, 6.0
g, 7.0

# 目标:只存储第二列数据到数组
f = open( "dat.txt", "r" )
n = countlines( f )
seekstart( f )

x = zeros(n)
for i = 1:n
    str1, str2 = split( readline( f ), "," )  # 读取每一行数据并用split函数将数据“剥离”开来
    x[i] = parse( str2 )  # 将字符串转化为数字
    println( x[i] )
end

close( f )

猜你喜欢

转载自blog.csdn.net/chd_lkl/article/details/81430082