Skip to content

Commit 2d69730

Browse files
committed
Add Parsing A CSV With Quotes In The Data as a ruby til
1 parent 560b924 commit 2d69730

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
99

1010
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1111

12-
_928 TILs and counting..._
12+
_929 TILs and counting..._
1313

1414
---
1515

@@ -752,6 +752,7 @@ _928 TILs and counting..._
752752
- [Or Operator Precedence](ruby/or-operator-precedence.md)
753753
- [Override The Initial Sequence Value](ruby/override-the-initial-sequence-value.md)
754754
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
755+
- [Parsing A CSV With Quotes In The Data](ruby/parsing-a-csv-with-quotes-in-the-data.md)
755756
- [Pass A Block To Count](ruby/pass-a-block-to-count.md)
756757
- [Passing Arbitrary Methods As Blocks](ruby/passing-arbitrary-methods-as-blocks.md)
757758
- [Passing Arguments To A Rake Task](ruby/passing-arguments-to-a-rake-task.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Parsing A CSV With Quotes In The Data
2+
3+
If a CSV contains unescaped quote characters--like you might find in a CSV full
4+
of measurement data--then Ruby's
5+
[CSV](https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html) library will
6+
be unable to parse it.
7+
8+
Here is what happens with a line of CSV about some lumber:
9+
10+
```ruby
11+
> require 'csv'
12+
> CSV.parse_line('oak,2",4"')
13+
CSV::MalformedCSVError (Illegal quoting in line 1.)
14+
```
15+
16+
However, we can enable some more lenient, liberal parsing of the data with the
17+
`liberal_parsing` argument.
18+
19+
```ruby
20+
> require 'csv'
21+
> CSV.parse_line('oak,2",4"', liberal_parsing: true)
22+
=> ["oak", "2\"", "4\""]
23+
```
24+
25+
[source](https://blog.bigbinary.com/2016/11/22/ruby-2-4-introduces-liberal_parsing-option-for-parsing-bad-csv-data.html)

0 commit comments

Comments
 (0)