gpt4 book ai didi

ruby - 在 Ruby 中打印带有空字段的行

转载 作者:数据小太阳 更新时间:2023-10-29 08:53:37 25 4
gpt4 key购买 nike

假设我有一个以 | 作为分隔符的 CSV 文件。我知道如何阅读和打印内容,但我不确定如何打印 Row 7 has an empty field。显示它是哪个字段会很好,但我现在关心的是先显示行,然后我可以尝试执行第二个任务。

require 'csv'
require 'pp'

CSV.open("exam.txt", 'r',"|") do |row|
pp row
end

最佳答案

require 'csv'
require 'pp'

i = 0
CSV.open("exam.txt", 'r',"|") do |row|
i += 1;
pp row
pp "Row " << i.to_s << " has an empty field" if row.include?(nil)
end

您可以使用类似row.index(nil) 的方法来查找nil 字段。阅读 EnumerableArray文档,您可能会找到一些东西。

关于ruby - 在 Ruby 中打印带有空字段的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6942907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com