File 为什么我无法从Netlogo读取txt文件中的字符串?
我正在尝试从txt文件中读取以下行: 工作1 1 15 25 90 3 1111 1100 0010 0110 1010 0 0 0 0 0 0 0 0 0 然而,我收到的所有时间:预期为常数(第1行,字符5) 在这种情况下,我有很多问题 A) 如何使netlogo读取字符串“Job1” B) 考虑到第10个数字是一个二进制数,我怎样才能使它的头变成一个字符串而不是一个数字呢 谢谢你的回答File 为什么我无法从Netlogo读取txt文件中的字符串?,file,text,netlogo,File,Text,Netlogo,我正在尝试从txt文件中读取以下行: 工作1 1 15 25 90 3 1111 1100 0010 0110 1010 0 0 0 0 0 0 0 0 0 然而,我收到的所有时间:预期为常数(第1行,字符5) 在这种情况下,我有很多问题 A) 如何使netlogo读取字符串“Job1” B) 考虑到第10个数字是一个二进制数,我怎样才能使它的头变成一个字符串而不是一个数字呢 谢谢你的回答 大猩猩迷我不太确定我是否真的得到了,你想得到什么。是否要将“txt文件”的所有元素作为字符串读取,但用空格
大猩猩迷我不太确定我是否真的得到了,你想得到什么。是否要将“txt文件”的所有元素作为字符串读取,但用空格分隔? 如果是,您可以尝试逐字符读取文件,以检查空格之间的字符串长度。然后再次浏览该文件并提取这些字符串。下面是如何实现的示例代码。也许还有更优雅的版本,但这一款非常适合我:
globals
[
char-list
char-counter
string-list
current-char
]
to read
set char-counter 0
set char-list []
set string-list []
set current-char 0
;; Open the file and go through it, char by char to check where the blank spaces are
file-open "in.txt"
while [file-at-end? = false]
[
;; Save current char
set current-char file-read-characters 1
;; Check if the char is a blank space...
ifelse (current-char != " ")
;; If not, increase the length of the current string
[
set char-counter char-counter + 1
]
;; If yes, save the length of the previous string, and reset the char-counter
[
set char-list lput char-counter char-list
set char-counter 0
]
]
file-close
;; Now read the file again and extract only the strings which are not blank spaces
file-open "in.txt"
let i 0
while [i < length char-list]
[
;; Read the next number of characters as defined by the previously created char-list
set string-list lput file-read-characters item i char-list string-list
;; Skip 1 space:
set current-char file-read-characters 1
;; Increase i
set i i + 1
]
file-close
end
globals
[
字符表
字符计数器
字符串列表
当前字符
]
阅读
设置字符计数器0
设置字符列表[]
设置字符串列表[]
将当前字符设置为0
;; 打开文件并通过它,char通过char检查空格是在哪里
文件打开“in.txt”
而[文件末尾?=false]
[
保存当前字符
将当前字符文件的读取字符设置为1
;检查字符是否为空白。。。
ifelse(当前字符!=“”)
;如果不是,则增加当前字符串的长度
[
设置字符计数器字符计数器+1
]
;如果是,则保存上一个字符串的长度,并重置字符计数器
[
设置字符列表lput字符计数器字符列表
设置字符计数器0
]
]
文件关闭
;; 现在再次读取该文件并仅提取非空格的字符串
文件打开“in.txt”
让我0
而[i
在此列表中搜索类似问题。还可以查看您用来尝试读取行的代码是什么?文件打开“file.txt”设置名称文件读取;;为了得到这份工作。设置代码文件读取;;获取0010,但它以数字形式获取。我需要它作为一根绳子。非常好,谢谢。