I'm trying to create a string literal representing a CSV file with quoted fields. The intended CSV looks like this:
"a","b"
"1","2"
Triple quotes work if I want a newline character at the end of the string:
julia> """
"a","b"
"1","2"
"""
"\"a\",\"b\"\n\"1\",\"2\"\n"
But if I try to make a string without the newline character at the end, then I get a syntax error:
julia> """
"a","b"
"1","2""""
ERROR: syntax: cannot juxtapose string literal
Is there a simple way to get around this?
As an aside, note that there is no syntax error when you start the string-literal with a quote:
julia> """"a","b"
"1","2"
"""
"\"a\",\"b\"\n\"1\",\"2\"\n"
The issue is that this by itself is a valid string literal:
"""
"a","b"
"1","2"""
When you follow that with another "
the parser thinks "woah, you can’t just follow a string with another string". You can force it to not consider the quote after 2
as part of a closing """
sequence by escaping it with \
:
"""
"a","b"
"1","2\""""
At the start of the string, there's no such issue since the first three "
characters are taken to start a string literal and the following "
must just be a quote character inside of the string, which is what you want.
I'm not sure what you would consider the best solution to be. The options are:
"
at the end;"""
on a separate line.The latter seems better to me, but it's your call.
See the Julia Docs for other examples on Triple-Quoted String Literals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With