在Netlogo中读取文件时忽略空行的最简单方法 [英] Easiest way to ignore blank lines when reading a file in Netlogo
本文介绍了在Netlogo中读取文件时忽略空行的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些代码可以读取名称文件并创建列表:
I have some code that reads a file of names and creates a list:
let who-file-name "world-health-field-surveillance.csv"
let who-file-name-dict csv:from-file who-file-name
let who-file-names sort [who] of names
let index 1 ;not 0, this removes the header in the csv
repeat length who-file-names [
file-open "world-health-field-surveillance.csv"
if file-at-end? [stop]
let entry (item 0 (item index who-file-name-dict))
if entry = "\n" [stop]
文件可能以一些空行结尾,或者文件的名称可能用换行符分隔,如下所示:
The file might end with some blank lines or its possible the file has names separated by a newline, like so:
Allman
Atkinson
Behlendorf
我想忽略任何仅包含空格的行.
我的示例代码不起作用. 如何在netlogo中做到这一点?
I want to ignore any lines that contain only whitespace.
My sample code doesn't work. How could I do this in netlogo?
推荐答案
您到底想做什么?如果我有一个看起来像这样的csv文件:
What are you trying to do, exactly? If I have a csv file that looks like this:
如果我运行以下代码:
extensions [ csv ]
to setup
ca
let example csv:from-file "example_names.csv"
print example
reset-ticks
end
我得到的列表输出如下:
I get a list output that looks like:
[[Allman] [Atkinson] [Behlendorf] [Belnich] [Cravit] [Court]]
这不是您所追求的吗?如果您只需要一个单级列表,则可以
Is that not what you're after? If you need just a single-level list, you can do
print reduce sentence example
获得
[Allman Atkinson Behlendorf Belnich Cravit Court]
这篇关于在Netlogo中读取文件时忽略空行的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文