gpt4 book ai didi

ruby - 在单个 csv.new() 行中创建一个带有标题的新 Ruby CSV 对象

转载 作者:数据小太阳 更新时间:2023-10-29 07:15:21 26 4
gpt4 key购买 nike

我正在尝试创建一个新的 CSV 对象,其中只有标题行,但直到我调用 read() 才设置标题:

[32] pry(main)> c = CSV.new("Keyword,Index,Page,Index in Page,Type,Title,URL", :headers => :first_row, :write_headers => true, :return_headers => true)
=> <#CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:"," row_sep:"\n" quote_char:"\"" headers:true>
[33] pry(main)> c.headers
=> true
[34] pry(main)> c.read
=> #<CSV::Table mode:col_or_row row_count:1>
[35] pry(main)> c.headers
=> ["Keyword", "Index", "Page", "Index in Page", "Type", "Title", "URL"]

这是为什么呢?为什么我不能用我的单个 CSV.new 行获得正常工作的 CSV 对象?

最佳答案

作为documentation会告诉您它将字符串视为文件的内容(即 StringIO),因此您仍然必须像读取任何其他 IO 源一样读取字符串。

如果你想显式设置 header ,你可以传递一个数组作为 :headers 参数。

关于ruby - 在单个 csv.new() 行中创建一个带有标题的新 Ruby CSV 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13631648/

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