Comments (10)
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.
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.
Could you try the following?
rows = [
CSV::Row.new(row0, row1),
CSV::Row.new(row0, row2)
]
from csv.
from csv.
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.
Ah-- I had made a typo when using your suggested changes: fixing the typo gives me the same results as you.
from csv.
OK. Is it expected result? Do we still have any problem?
from csv.
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.
Feel free to close this issue if this is intended behaviour.
from csv.
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)
- New bugfix version for the changes on master HOT 1
- CSV.generate is not working with Rails 7 HOT 18
- `CSV::Row` pattern matching `Symbol` assumption HOT 1
- :date_time converter fails to recognize "YYYY-MM-DD HH:MM" HOT 7
- Add quoted information to CSV::FieldInfo HOT 1
- ArgumentError: unknown encoding name - iso-8859-1|utf-8 HOT 2
- Feature Request: Generate CSV String from Array HOT 5
- #eof? method returning wrong value when it's used on a csv file HOT 3
- #eof? method returning wrong value when it's used on a csv file with #each, #map, #filter HOT 1
- row access method(like .first, .count, .map) remove row unintentionally HOT 2
- New release for Ruby 3.2 HOT 7
- How about GH releases generated by `gh release create --generate-notes` HOT 5
- feature: add option to limit length of strings HOT 3
- Suggestion to add `sep` option HOT 1
- Broken links in documention HOT 11
- Duplicated last line in CSV.foreach HOT 14
- Recipes not copied downstream HOT 6
- A fiber to execute ':heder_converters' has been changed since v3.2.6 HOT 1
- liberal parsing does not split column as expected with quote at end of column HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csv.