没有将nil隐式转换为String错误

安吉拉·琼侬

我有一个ruby脚本,它将通过获取和合并另一个文件中的值来创建两个文件。

#Resources
require 'rubygems'
require 'csv'

col_date = []
col_constant1 = []
col_constant2 = []
col_appYear = []
col_statsDesc = []
col_keyStats =[]
col_weeklyTotal=[]


weekly_total = []

fname = "finalStats.csv" #variable for capture file
        finalStatsFile = File.open(fname, "w") #write to capture file
fname2 = "weeklyStats.csv"
        weeklyStatsFile = File.open(fname2, "w")    
CSV.foreach('compareData.csv', converters: :numeric) do |row|
    weekly_total << row[0] - row[1]

    weekly_total.each do |data| 
    data << weekly_total.shift 
     weeklyStatsFile.puts data
end 
end

#retrieve stats from original document 
 CSV.foreach("autoCapture.csv") {|row| col_date << row[0]}
 CSV.foreach("autoCapture.csv") {|row| col_constant1 << row[1]}
 CSV.foreach("autoCapture.csv") {|row| col_appYear << row[2]}
 CSV.foreach("autoCapture.csv") {|row| col_statsDesc << row[3]}
 CSV.foreach("autoCapture.csv") {|row| col_constant2 << row[4]}
 CSV.foreach("autoCapture.csv") {|row| col_keyStats << row[5]}
 CSV.foreach("weeklyStats.csv") {|row| col_weeklyTotal << row[0]}



  col_date.zip(col_constant1, col_appYear, col_statsDesc, col_constant2, col_keyStats, col_weeklyTotal).each do |col_date, col_constant1, col_appYear, col_statsDesc, col_constant2, 
  col_keyStats, col_weeklyTotal|

  finalStatsFile.puts col_date+", "+col_constant1+", "+ col_appYear+", "+col_statsDesc+", "+col_constant2+", "+col_keyStats+", "+col_weeklyTotal

 end

在一个文件中,我希望从行[0]中的值中减去行[1]中的值,以创建一个新的“ weekly_total”值。然后,我将这个值数组输出到一个名为weeklyStats.csv的文件中。这将输出一列值优良的列。

但是,我想将这些值与另一个文件(autoCapture.csv)中的另一个值连接起来,当我尝试将它们压缩为数组以便它们在相应的行中读取时,出现错误:

weeklyStats_csv.rb:42:in `+': no implicit conversion of nil into String (TypeError)
    from weeklyStats_csv.rb:42:in `block in <main>'
    from weeklyStats_csv.rb:40:in `each'
    from weeklyStats_csv.rb:40:in `<main>'

我收集到这意味着,如果值之一为nil,则数组zip将不会捕获异常,因此无法转换为字符串。问题是,我曾尝试将weekly_total转换为字符串和数组,因为我认为这可能是问题所在(类型不匹配),但我只是不知道从何而来。有人可以帮忙吗?

阿列克谢·马蒂什金(Alexei Matiushkin)

字符串中的(或多个)值之一

finalStatsFile.puts col_date+", "+col_constant1+", "+ col_appYear+", "+col_statsDesc+", "+col_constant2+", "+col_keyStats+", "+col_weeklyTotal

成为nil要修复输出,您应该将它们显式转换为字符串:

finalStatsFile.puts col_date.to_s + ", " + 
                    col_constant1.to_s + ", " + 
                    col_appYear.to_s + ", " + 
                    col_statsDesc.to_s + ", " +
                    col_constant2.to_s + ", " +
                    col_keyStats.to_s + ", " + 
                    col_weeklyTotal.to_s 

顺便说一句,整个条款可能会以更糟糕的方式重写:

finalStatsFile.puts [ col_date,
                      col_constant1,
                      col_appYear,
                      col_statsDesc,
                      col_constant2,
                      col_keyStats,
                      col_weeklyTotal ].map(&:to_s).join(', ')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:没有将 nil 隐式转换为 String

全新安装的 Ruby 错误(没有将 nil 隐式转换为 String)

使用没有 => 的哈希时,没有将 nil 隐式转换为 String

控制器规格.reload错误:没有将nil隐式转换为Hash

TypeError:没有使用gsub将nil隐式转换为String

TypeError(没有将nil隐式转换为String),仅在生产中

Rails 使用多个数据库 - 没有将 nil 隐式转换为 String

eRuby - 在 if 语句中比较数字时没有将 nil 隐式转换为 String

无法更新Calabash-sandbox Ruby“没有将nil隐式转换为String”

没有将HTTParty :: Response隐式转换为String

没有将Array隐式转换为String吗?

没有将Hash隐式转换为String(TypeError)

没有将String隐式转换为Integer(TypeError)

没有将 ActiveSupport::HashWithIndifferentAccess 隐式转换为 String?

+:没有将Integer隐式转换为String(TypeError)

Ruby错误-'+':没有将Fixnum隐式转换为String(TypeError)

Ruby错误:“没有将Fixnum隐式转换为String”

如何解决JSON解析错误:“没有将String隐式转换为Integrer”?

TypeError:没有将nil隐式转换为String。utils.rb:24:in'quote_ident'

TypeError(没有将nil隐式转换为String)生产环境上的Rails devise-jwt gem

heroku toolbelt`initialize':没有将nil隐式转换为Hash(TypeError)

没有使用ruby将nil隐式转换为整数

C# 返回错误“无法将类型‘int’隐式转换为‘string’”,即使没有给出 int

ROR +无法将数据存储在数据库中(TypeError异常:没有将nil隐式转换为String)

Ruby 类型错误:没有将符号隐式转换为整数

嵌套属性错误:没有将字符串隐式转换为整数

没有将 Struct 隐式转换为 Array

没有将ActiveSupport :: TimeWithZone隐式转换为Integer

没有将ActiveSupport :: SafeBuffer隐式转换为Integer