Avoid double quote confusion when generating CSV files? - ruby

I am pushing some strings to a CSV file:
csv_string = CSV.generate({col_sep: ";", row_sep: "\n"}) do |csv|
csv << ["101-41", "Sparri, \"Violet\" (rod) (1 bunt á 10 stk.)"]
end
When the CSV file is finally generated a regular Excel installation will display the content apparently correctly, but a closer look through a plain text editor (like Sublime Text) shows that the double quotes create confusion:
10-41;"Sparri, ""Violet"" (rod) (1 bunt á 10 stk.)"
How can I avoid this? Should I convert double quotes into something different before pushing to the CSV file?

This is correct, a double quote is escaped with another double quote. From RFC 4180:
If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:
"aaa","b""bb","ccc"

CSV is no offical standard, strange as it is.
There are the RFC 4180 guidelines that state about double quotes
5. Each field may or may not be enclosed in double quotes (however
some programs, such as Microsoft Excel, do not use double quotes
at all). If fields are not enclosed with double quotes, then
double quotes may not appear inside the fields. For example:
"aaa","bbb","ccc" CRLF
zzz,yyy,xxx
6. Fields containing line breaks (CRLF), double quotes, and commas
should be enclosed in double-quotes. For example:
"aaa","b CRLF
bb","ccc" CRLF
zzz,yyy,xxx
7. If double-quotes are used to enclose fields, then a double-quote
appearing inside a field must be escaped by preceding it with
another double quote. For example:
"aaa","b""bb","ccc"
So your formatting is correct and aslong as Excel shows the file correctly I wouldn't mind about the double "double quotes"

Related

Keep text delimiters after !!str "some text" or !!str 'some text'

I have some data to be used to generate SQL, therefore it is important which text delimiters are used (single quotes ' delimits string literal but double quotes " delimit identifiers, at least in Oracle db).
For load procedure generator I used this
someKey: !!str 'Some SQL text'
and expected that someKey would contain the whole string including single quotes: 'Some SQL text'.
However, js-yaml.safeLoad() interprets the data as Some SQL text which is not what I wanted.
The workaround is easy, I can put the literal into additional quotes:
someKey: "'Some SQL text'"
which gives the expected result. However, I am not quite sure why in that case do we need !!str tag in YAML if it does virtually nothing (it is useful only for explicit interpretation number literals, true, false and null) and it is actually almost the same as putting double quotes around the text.
I would prefer to post this into some YAML-spec-related forum but it seems there is none.
Apart from the standard workaround, is there any trick that would do what I originally wanted, i.e. interpret any content after object key as string (+trimming off any initial and trailing spaces) without dealing with double quotes?
In YAML tag !!str is a predifened denoting a string scalar. If you specify that then even things that without that tag (or without quotes) would not be considered a string scalar, like 123, True or null.
Some string scalars need quotes e.g. if they start with a quote or double quote, if special characters need backslash espacing, or if there is a : (colon, space) in the string (which could confuse the parser to intrepret the string scalar as a key-value pair.
However putting !!str before something doesn't make it quoted (which should be obvious as it doesn't define what kind of quoting and single quoted scalars have vastly different rules from double quoted scalars).
Your workaround is not a workaround, that is just one of the ways in YAML you can specify a string scalar that starts and ends with a single quote. Another way is:
someKey: |-
'Some SQL text'
Within literal block style scalars quotes (single or double) are interpreted as is even at the beginning of the scalar. The - makes sure you don't get an extra newline after the final '

Escape characters in teradata jdbc connection string

I have a teradata database name that contains a dash character -.
Searched the web but in vain. Does somebody know how can you escape the special characters in jdbc connection string? The string looks as follows:
jdbc:teradata://HostName/DATABASE=Database-Name
When I create a connection with this url I get syntax error. Also tried to put database parameter in single or double quotes, and to surround the special charachers with { }.
Thanks for help!
Finally found the answer here: https://jira.talendforge.org/browse/TDI-18863. The correct way is to enclose both parameter name and value in single quotes:
jdbc:teradata://HostName/'DATABASE=Database-Name'
Update: No, this does not work, see comment below.
Answering my own question:
My problem was that I didn't realise that my database name had some trailing whitespaces in the end.
TeraDriver uses single quotes to escape spaces and commas. This means that the database name should be in single quotes. If there are no single quotes, spaces and commas are considered to be the end of parameter value. If there are single quotes in database name, they should be presented as two single quote characters.
'Database-Name '
Whatever is within single quotes will be used with sql query: "database Database-Name". To escape '-' we need double quotes. So both single and double quotes in correct order should be used:
"jdbc:teradata://HostName/DATABASE='\"Database-Name\"'"
Have you tried a \ character which is supposed to be an escape character in Java?
jdbc:teradata://HostName/DATABASE=Database\-Name

What's the point of escaping a single backslash in single quotes in Ruby?

I don't quite understand what's the point of escaping a single backslash when you have a string in single quotes in Ruby. Why does Ruby treats backslashes 'differently'?
backslashes are an escape character so if you were to write '\' would think you were trying to escape the '.
Otherwise if it treated single character strings differentls you wanted to write ' you would have to use double quotes, which will quickly get harder to maintain when you need to remember which quotes to use when.
If your question is actually "What is the point of the language design requiring us to escape a backslash in single (as opposed to double) quotes", then that is to allow single quotes to appear within a string literal written with single quotes. In order to do that, there must be an escape character for single quotes, which is the backslash, and then, the escape character itself needs to be escaped.

Custom code highlight Notepad++

I am creating a custom code highlight for notepad++. What I want to do is the following:
some fieldnames are writen in the code with a ' in front of their name, for exampe
if 'variable = "test" then ...
I would like to highlight these words, but notepad++ does not seem to allow a delimiter starting with ' and ending with a space, not does it allow space as an escape character. Also, using ' as a keyword and enabling prefix mode has no effect. Anyone has a suggestion? Should I use another expression to let notepad recognise the space/' ?
Thanks in advance!
If you only need to highlight a single word, you can use a keyword in prefix mode. However when using single or double quotes in a keyword, they need to be escaped with a backslash. So your keyword would be:
\'
This may not be possible in notepad++. I can get the behavior you want using a character other than a single quote, like a back-tic, but it doesn't seem to work with single or double quotes. I suspect those characters are treated special within the syntax highlighter.

Double Quotes in ASCII

What is the ASCII number for the double quote? (")
Also, is there a link to a list anywhere?
Finally, how do you enter it in the C family (esp. C#)
The ASCII code for the quotation mark is 34.
There are plenty of ASCII tables on the web. Note that some describe the standard 7-bit ASCII code, while others describe various 8-bit extensions that are super-sets of ASCII.
To put quotation marks in a string, you escape it using a backslash:
string msg = "Let's just call it a \"duck\" and be done with it.";
To put a quotation mark in a character literal, you don't need to escape it:
char quotationMark = '"';
Note: Strings and characters in C# are not ASCII, they are Unicode. As Unicode is a superset of ASCII the codes are still usable though. You would need a Unicode character table to look up some characters, but an ASCII table works fine for the most common characters.
It's 34. And you can find a list on Wikipedia.
yes, the answer the 34
In order to find the ascii value for special character and other alpha character I'm writing here small vbscript. In a note pad, write the below script save as abc.vbs(any name with extention .vbs) double click on the file to execute and you can see double quotes for 34.
For i=0 to 150
msgbox i&"="&char(i)
next
you are never going to need only 1 quote, right?
so I declare a CHAR variable i.e
char DoubleQuote;
then drop in a double quote
Convert.ToChar(34);
so use the variable DoubleQuote where you need it
works in SQL to generate dynamic SQL but there you need
DECLARE #SingleQuote CHAR(1)
and
SET #SingleQuote=CHAR(39)

Resources