【问题标题】:Easiest way to ignore blank lines when reading a file in Netlogo在 Netlogo 中读取文件时忽略空行的最简单方法
【发布时间】:2018-07-13 20:08:49
【问题描述】:

我有一些代码可以读取名称文件并创建一个列表:

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]

文件可能以一些空行结尾,或者文件名称可能由换行符分隔,如下所示:

Allman
Atkinson

Behlendorf 

我想忽略任何只包含空格的行。

我的示例代码不起作用。 我怎么能在 netlogo 中做到这一点?

【问题讨论】:

    标签: netlogo


    【解决方案1】:

    你到底想做什么?如果我有一个如下所示的 csv 文件:

    如果我运行这段代码:

    extensions [ csv ]
    
    to setup
      ca
      let example csv:from-file "example_names.csv"
      print example
      reset-ticks
    end
    

    我得到一个列表输出,如下所示:

    [[Allman] [Atkinson] [Behlendorf] [Belnich] [Cravit] [Court]]
    

    这不是你所追求的吗?如果你只需要一个单级列表,你可以这样做

    print reduce sentence example
    

    得到

    [Allman Atkinson Behlendorf Belnich Cravit Court]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 1970-01-01
      • 2017-09-15
      相关资源
      最近更新 更多