GithubHelp home page GithubHelp logo

Comments (10)

kou avatar kou commented on August 23, 2024

Is it just happened by a typo?

--- /tmp/a.rb.orig	2023-11-25 07:39:48.942802182 +0900
+++ /tmp/a.rb	2023-11-25 07:39:59.030796716 +0900
@@ -16,7 +16,7 @@
 tablestring = <<-EOT
 header0,header 1,Header 2
 r1value0,r1value2,r1value3
-r2value0,r2value2,r3value3
+r2value0,r2value2,r2value3
 EOT
 
 

from csv.

Zopolis4 avatar Zopolis4 commented on August 23, 2024

Ah. Yes, that certainly cuts down on a few of the issues. Some still remain though, I've attatched a testcase with the remaining ones.

require 'csv'

# Array of strings making up the equivalent of a 3-row CSV file
row0 = [ 'header0', 'header 1', 'Header 2' ]
row1 = [ 'r1value0', 'r1value2', 'r1value3' ]
row2 = [ 'r2value0', 'r2value2', 'r2value3' ]

# Array of rows made from the previous array of strings
rows = [
  CSV::Row.new(row0, [], header_row = true),
  CSV::Row.new([], row1),
  CSV::Row.new([], row2)
]

# Heredoc for that same CSV file
tablestring = <<-EOT
header0,header 1,Header 2
r1value0,r1value2,r1value3
r2value0,r2value2,r2value3
EOT


# Table made using CSV::Table.new on the array of rows
table1 = CSV::Table.new(rows, headers: row0)

# Table made using CSV.parse on the CSV heredoc
table2 = CSV.parse(tablestring, headers: true)

# Table made using the previous table converted toputs "table1.to_csv & tablestring: #{table1.to_csv == tablestring}" a CSV string (i.e. what should be equivalent to the CSV heredoc)
table3 = CSV.parse(table1.to_csv, headers: true)

puts "Test whether the headers of the tables are the same (they should be)"
puts "table1.headers & table2.headers: #{table1.headers == table2.headers}"
puts "table1.headers & table3.headers: #{table1.headers == table3.headers}"
puts "table2.headers & table3.headers: #{table2.headers == table3.headers}"

puts "\n\nAttempt to access the first column of each table"
puts "table1['header0']: #{table1['header0']}"
puts "table2['header0']: #{table2['header0']}"
puts "table3['header0']: #{table3['header0']}"

puts "\n\nAttempt to access the first row of each table"
puts "table1[0]: #{table1[0]}"
puts "table2[0]: #{table2[0]}"
puts "table3[0]: #{table3[0]}"

puts "\n\nTest whether each of the tables are the same"
puts "table1 == table2: #{table1 == table2}"
puts "table1 == table3: #{table1 == table3}"
puts "table2 == table3: #{table2 == table3}"

puts "\n\nPrint out each table"
puts "table1: \n#{table1}"
puts "table2: \n#{table2}"
puts "table3: \n#{table3}"

from csv.

kou avatar kou commented on August 23, 2024

Could you try the following?

rows = [
  CSV::Row.new(row0, row1),
  CSV::Row.new(row0, row2)
]

from csv.

Zopolis4 avatar Zopolis4 commented on August 23, 2024

That gives me this result:
image

from csv.

kou avatar kou commented on August 23, 2024

Here is my result:

$ cat /tmp/a.rb
require 'csv'

# Array of strings making up the equivalent of a 3-row CSV file
row0 = [ 'header0', 'header 1', 'Header 2' ]
row1 = [ 'r1value0', 'r1value2', 'r1value3' ]
row2 = [ 'r2value0', 'r2value2', 'r2value3' ]

# Array of rows made from the previous array of strings
rows = [
  CSV::Row.new(row0, row1),
  CSV::Row.new(row0, row2)
]

# Heredoc for that same CSV file
tablestring = <<-EOT
header0,header 1,Header 2
r1value0,r1value2,r1value3
r2value0,r2value2,r2value3
EOT


# Table made using CSV::Table.new on the array of rows
table1 = CSV::Table.new(rows, headers: row0)

# Table made using CSV.parse on the CSV heredoc
table2 = CSV.parse(tablestring, headers: true)

# Table made using the previous table converted toputs "table1.to_csv & tablestring: #{table1.to_csv == tablestring}" a CSV string (i.e. what should be equivalent to the CSV heredoc)
table3 = CSV.parse(table1.to_csv, headers: true)

puts "Test whether the headers of the tables are the same (they should be)"
puts "table1.headers & table2.headers: #{table1.headers == table2.headers}"
puts "table1.headers & table3.headers: #{table1.headers == table3.headers}"
puts "table2.headers & table3.headers: #{table2.headers == table3.headers}"

puts "\n\nAttempt to access the first column of each table"
puts "table1['header0']: #{table1['header0']}"
puts "table2['header0']: #{table2['header0']}"
puts "table3['header0']: #{table3['header0']}"

puts "\n\nAttempt to access the first row of each table"
puts "table1[0]: #{table1[0]}"
puts "table2[0]: #{table2[0]}"
puts "table3[0]: #{table3[0]}"

puts "\n\nTest whether each of the tables are the same"
puts "table1 == table2: #{table1 == table2}"
puts "table1 == table3: #{table1 == table3}"
puts "table2 == table3: #{table2 == table3}"

puts "\n\nPrint out each table"
puts "table1: \n#{table1}"
puts "table2: \n#{table2}"
puts "table3: \n#{table3}"
$ ruby -v /tmp/a.rb
ruby 3.3.0dev (2023-09-22T02:25:53Z master fb7a2ddb4b) [x86_64-linux]
Test whether the headers of the tables are the same (they should be)
table1.headers & table2.headers: true
table1.headers & table3.headers: true
table2.headers & table3.headers: true


Attempt to access the first column of each table
table1['header0']: ["r1value0", "r2value0"]
table2['header0']: ["r1value0", "r2value0"]
table3['header0']: ["r1value0", "r2value0"]


Attempt to access the first row of each table
table1[0]: r1value0,r1value2,r1value3
table2[0]: r1value0,r1value2,r1value3
table3[0]: r1value0,r1value2,r1value3


Test whether each of the tables are the same
table1 == table2: true
table1 == table3: true
table2 == table3: true


Print out each table
table1: 
header0,header 1,Header 2
r1value0,r1value2,r1value3
r2value0,r2value2,r2value3
table2: 
header0,header 1,Header 2
r1value0,r1value2,r1value3
r2value0,r2value2,r2value3
table3: 
header0,header 1,Header 2
r1value0,r1value2,r1value3
r2value0,r2value2,r2value3

from csv.

Zopolis4 avatar Zopolis4 commented on August 23, 2024

Ah-- I had made a typo when using your suggested changes: fixing the typo gives me the same results as you.

from csv.

kou avatar kou commented on August 23, 2024

OK. Is it expected result? Do we still have any problem?

from csv.

Zopolis4 avatar Zopolis4 commented on August 23, 2024

That leaves us here:

This doesn't work:

rows = [
  CSV::Row.new(row0, [], header_row = true),
  CSV::Row.new([], row1),
  CSV::Row.new([], row2)
]

This does:

rows = [
  CSV::Row.new(row0, row1),
  CSV::Row.new(row0, row2)
]

If we run the broken one through CSV.parse it does work.

To me it looks like the issue is that CSV.parse accepts things that CSV::Table.new will not work properly with.

I'm not sure whether that is intended behaviour or an issue, though.

from csv.

Zopolis4 avatar Zopolis4 commented on August 23, 2024

Feel free to close this issue if this is intended behaviour.

from csv.

kou avatar kou commented on August 23, 2024

CSV.parse(table1.to_csv, headers: true) doesn't have any problem. Because it receives CSV text not CSV::Table.

If we want to avoid the wrong CSV::Table.new call, we can add a validation for the case. But I think that we don't need it for now.

from csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.