在Julia逐行阅读 [英] Reading line by line in Julia

查看:149
本文介绍了在Julia逐行阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从文件中读取每行包含一些整数



但是,当我给这样的

  f = open(data.txt)
a = readline(f)
arr = int64 []
push!(arr,int (a))

我得到

 ERROR:no方法getindex(函数)$ b $在include_from_node1在loading.jl:120 


解决方案

错误来自 int64 [] ,因为 int64 code>是一个函数,你试图用 [] 来索引它。要创建 Int64 (注意大小写)的数组,您应该使用 arr = Int64 []

您的代码中的另一个问题是 int(a) - 因为您有一个 Int64 ,你也应该在解析时指定相同的类型,例如, push!(arr,parseint(Int64,a)) p>

I am trying to read from a file where each line contains some integer

But when I gave like this

f=open("data.txt")
a=readline(f)
arr=int64[]
push!(arr,int(a))

I am getting

ERROR: no method getindex(Function)
 in include_from_node1 at loading.jl:120

解决方案

The error comes from int64[], since int64 is a function and you are trying to index it with []. To create an array of Int64 (note the case), you should use, e.g., arr = Int64[].

Another problem in your code is the int(a) - since you have an array of Int64, you should also specify the same type when parsing, e.g., push!(arr,parseint(Int64,a))

这篇关于在Julia逐行阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆