CSV.read Illegal quoting in line x
I had this problem in a line like 123,456,a"b"c
The problem is the CSV parser is expecting "
, if they appear, to entirely surround the comma-delimited text.
Solution use a quote character besides "
that I was sure would not appear in my data:
CSV.read(filename, :quote_char => "|")
Rescue CSV::MalformedCsvError: Illegal quoting in line n
Your solution works. The expected result resides in the variable my_array.
RAILS 3 CSV Illegal quoting is a lie
This part of your CSV is at fault:
46273 "[O/H 15/02] B270 W31 ""TEXT TEXT 2 X TEXT SWITC" SOME_TEXT
At least one of these parts has a stray space:
46273 "
" SOME_TEXT
I'd guess that the "3" and the double are supposed to be separated by one or more tabs but there is a space before the quote. Or, there is a space after the quote on the other end when there are only supposed to be tabs between the closing quote and the "S".
CSV escapes double quotes by double them so this:
"[O/H 15/02] B270 W31 ""TEXT TEXT 2 X TEXT SWITC"
is supposed to be a single filed that contains an embedded quote:
[O/H 15/02] B270 W31 "TEXT TEXT 2 X TEXT SWITC
If you have a space before the first quote or after the last quote then, since your fields are tab delimited, you have an unescaped double quote inside a field and that's where your "illegal quoting" error comes from.
Try sending your CSV file through cat -t
(which should represent tabs as ^I
) to find where the stray space is.
Logstash MalformedCSVError: Illegal quoting in line | how to configure logstash conf to read multi column mutiline CSV?
By starting Logstash with a pipeline definition /softwares/logstash-7.6.0/config/*.conf
you probably have many configuration files that interfere with each other (i.e. several different inputs, filters and outputs). All those configuration files are merged into a single configuration.
Read this article in order to learn how to best organize your Logstash pipelines.
In the meantime, you can simply start your Logstash from the command-line like this:
bin/logstash -f /softwares/logstash-7.6.0/config/cust.conf
How can I read CSV with strange quoting in ruby?
This is an invalid csv file. If you have access to the source, you could (ask to) generate the data as follows:
col1,"col ""two""","col,3"
If not, the only option is to parse the data yourself:
pseudocode:
while(read_line) {
bool InsideQuotes = false
for each_char_in_line {
if(char == doublequote)
InsideQuotes = !InsideQuotes
if(char == ',' and !InsideQuotes)
// separator found - process field
}
}
This will also take care of escaped quotes like in col1,"col ""two""","col,3"
.
If the file contains multiline fields, some more work has to be done.
Ruby CSV.parse very picky when encountering quotes
This is correct behavior. It's not being fragile.
Your comma after "four" is ending the field, and the next field starts immediately with the space.
You can't validly put a quote in the middle of a field (without escaping it).
Related Topics
Rails: Postgres Permission Denied to Create Database on Rake Db:Create:All
What's the Difference Between Gem_Home and Gem_Path
Database Configuration Does Not Specify Adapter
Ruby Sleep or Delay Less Than a Second
Where to Put Ruby Helper Methods for Rails Controllers
How to Convert a Ruby Hash So That All of Its Keys Are Symbols
Ruby: How to Convert a String to Boolean
How to Access the Rack Environment from Within Rails
Difference Between @Instance_Variable and Attr_Accessor
Routing Nested Resources in Rails 3
Ruby - Lexical Scope VS Inheritance
Best Practice for Rails App to Run a Long Task in the Background