Easiest way to ignore blank lines when reading a file in Netlogo

让人想犯罪 __ 提交于 2020-01-06 06:52:24

问题


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 

I want to ignore any lines that contain only whitespace.

My sample code doesn't work. How could I do this in netlogo?


回答1:


What are you trying to do, exactly? If I have a csv file that looks like this:

If I run this code:

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

to get

[Allman Atkinson Behlendorf Belnich Cravit Court]


来源:https://stackoverflow.com/questions/51332183/easiest-way-to-ignore-blank-lines-when-reading-a-file-in-netlogo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!