why does using "\" shows error in jython - jython

I am trying to use a copy command for Windows and we have directories such as c:\oracle.
While trying to execute one such, we get the following error:
source_file=folder+"\"
^
SyntaxError: Lexical error at line 17, column 23. Encountered: "\r" (13), after : ""
Here folder is my path of c:\oracle and while trying to add file to it like:
source=folder+"\"+src_file
I am not able to do so. Any suggestion on how to solve this issue?
I tried with / but my copy windows calling source in os.command is getting "the syntax is incorrect" and the only way to solve it is to use \ but I am getting the above error in doing so.
Please suggest. Thanks for your help
Thanks.

Short answer:
You need:
source_file = folder + "\\" + src_file
Long answer:
The problem with
source_file = folder + "\" + src_file
is that \ is the escape character. What it's doing in this particular case is escaping the " so that it's treated as a character of the string rather than the string terminator, similar to:
source_file = folder + "X + src_file
which would have the same problem.
In other words, you're trying to construct a string consisting of ", some other text and the end of line (\r, the carriage return character). That's where your error is coming from:
Encountered: "\r" (13)

Paxdiablo is absolutely correct about why \ isn't working for you. However, you could also solve your problem by using os.path.normpath instead of trying to construct the proper platform-specific path characters yourself.

In all programming languages I know of, you can't put a quote inside a string like this: "this is a quote: "." The reason for this is that the first quote opens the string, the second then closes it (!), and then the third one opens another string - with the following two problems:
whatever is between the quotes #2 and #3 is probably not valid code;
the quote #3 is probably not being closed.
There are two common mechanisms of solving this: doubling and escaping. Escaping is far more common, and what it means is you put a special character (usually \) in front of characters that you don't want to be interpreted in their usual value. Thus, "no, *this* is a quote: \"." is a proper string, where the quote #2 is not closing the string - and the character \ does not appear.
However, now you have another problem - how do you actually make the escape character appear in a string? Simple: escape it! "This is an escape: \\!" is how you do it: the backslash #1 is the escape character, and the backslash #2 is the escapee: it will not be interpreted with its usual escape semantics, but as a simple backslash character.
Thus, your line should say this:
source=folder+"\\"+src_file
BTW: upvote for both #paxdiablo (who got in before my diatribe) and #Nick (who has a proper Pythonic way to do what you want to do)

Related

Escaping custom character in objective-c

I am working on macOS, not iOS, XCode 11.
My app allows in a specific location to enter text. This text can be anything. Once done it exports a csv which will be passed to an external process i cannot influence.
The issue: the external process uses semicolon ";" as a separator (csv is separated differently). If the user writes semicolon the external process will fail.
If I manually add an escaping backslash before each semicolon to the csv and then pass it to the external app it works.
What I need: having each semicolon escaped with ONE backslash in the final csv
What I tried
Escaping the whole text with quotation marks - fail
Escaping semicolons in objective-c before writing csv by trying
stringByReplacingOccurrencesOfString (look for #";" replace with #"\;" - compiler throws a warning that escape character is unknown - fail
Appreciate any help
UPDATE:
I also tried to set a double backslash like #Corbell mentioned but this leads in a double backslash in the exported CSV -> fail
I also tried to set a single backslash by using its unicode character:
[NSString stringWithFormat:#"%C;",0x5C]; --> "\\;"
Also failed and produces two backslashes in the final CSV (where i need ONE only).
In your stringByReplacingOccurencesOfString call, second parameter, try escaping your backslash with a backslash to make it a literal character to insert, i.e. #"\\;" - otherwise the compiler thinks you're trying to specify #"\;" as an escape sequence (backslash-semicolon) which is invalid.
Solved. It was the CSV Parser that added additional escaping characters. Once solved that it worked like a charm.

How to put a double quote within two double quotes in a batch file and how to escape double quote in VBA script file

So I want to echo out a line which contains just three double quotes. If I print two double quotes it just works fine and prints it (I believe the reason is that there is a corresponding closing quote) and double quotes in multiples of 2 work just fine but I'm unable to echo an odd number of them.
And from my searching the escape sequence character for double quote is double quote itself and it isn't getting me the desired result.
The command I'm running:
echo Item.Subject = Replace(Item.Subject, """, "-")>> try.xyz
expecting """ with this.
I've tried
"\""
"\\""
"^""
I am generating a bas file for importing in Outlook. And MC ND has corrected my mistake in the VBA file that I'm generating too. The proper way is to print 4 double quotes to do the escaping for the double quote from VBA script point of view and hence normal printing of 4 double quotes would solve my problem. The second double quote would be to escape the third one and the first and the last are to mention the replacement string from MC ND's answer.
Thanks a lot MC ND you've enlightened me. :-P
PS:
MC ND has given a much detailed explanation than this question deserved, so I extended the question.
Assumption: your batch code is generating a vbs file (or similar)
Your problem is not to echo the three quotes. Your problem is that in VBScript a double quote enclosed in double quotes needs to be escaped (from the vbs point of view), and to do so you need to double it, that is, you need not three but four quotes, the two that delimit the string and the two that means a escaped double quote
>>"try.xyz" echo Item.Subject = Replace(Item.Subject, """", "-")
note: I've moved the redirectioin to the start of the line to avoid problems with lines that could end in digits that could be parsed as a request to redirect a specific numbered stream
Assumption: Your code is NOT generating a vbs file and the quote escaping is what you indicate.
Then your problem is that the odd number of quotes is fooling the batch parser that keep an unclosed string that will include the redirection inside the data to echo, instead of writing to the output file
You can change the redirection to the start of the line as in the previous sample (at in this case it will work, the echo is still not correct), or, you can escape (from the batch point of view) the first quote so it is not considered double quote and to end with a properly quoted string, not including the redirection in the output
>>"try.xyz" Item.Subject = Replace(Item.Subject, """, "-")
echo Item.Subject = Replace(Item.Subject, ^""", "-") >> "try.xyz"
rem Best use both
>>"try.xyz" Item.Subject = Replace(Item.Subject, ^""", "-")
Why not to escape the inner quote? As it already is inside a quoted string, the escape will fail. We can escape the first or the last (in this case), not the inner one.
Assumption: Your code is not enclosed in parenthesis (for, if, ... ) inside the batch code
Then your code do not have any other problem,BUT if the assumption is wrong, then there is an additional problem. The closing parenthesis you are echoing will be seen by the batch parser as the closing parenthesis of the current block. To avoid it, you need to escape (from the batch parser point of view) the closing parenthesis.
(
....
>>"try.xyz" echo Item.Subject = Replace(Item.Subject, """", "-"^)
....
)

handling strings with \n in plain text e-mail

I have a column in my database that contains a string like this:
"Warning set for 7 days.\nCritical Notice - Last Time Machine backup was 118 days ago at 2012-11-16 20:40:52\nLast Time Machine Destination was FreeAgent GoFlex Drive\n\nDefined Destinations:\nDestination Name: FreeAgent GoFlex Drive\nBackup Path: Not Specified\nLatest Backup: 2012-11-17"
I am displaying this data in an e-mail to users. I have be able to easily format the field in my html e-mails perfectly by doing the following:
simple_format(#servicedata.service_exit_details.gsub('\n', '<br>'))
The above code replaces the "\n" with "<br>" tags and simple_format handles the rest.
My issues arises with how to format it properly in the plain text template. Initially I thought I could just call the column, seeing as it has "\n" I assumed the plain text would interpret and all would be well. However this simply spits out the string with "\n" intact just as displayed above rather than created line breaks as desired.
In an attempt to find a way to parse the string so the line breaks are acknowledged. I have tried:
#servicedata.service_exit_details.gsub('\n', '"\r\n"')
#servicedata.service_exit_details.gsub('\n', '\r\n')
raw #servicedata.service_exit_details
markdown(#servicedata.service_exit_details, autolinks: false) # with all the necessary markdown setup
simple_format(#servicedata.service_exit_details.html_safe)
none of which worked.
Can anyone tell me what I'm doing wrong or how I can make this work?
What I want is for the plain text to acknowledge the line breaks and format the string as follows:
Warning set for 7 days.
Critical Notice - Last Time Machine backup was 118 days ago at 2012-11-16 20:40:52
Last Time Machine Destination was FreeAgent GoFlex Drive
Defined Destinations:
Destination Name: FreeAgent GoFlex Drive
Backup Path: Not Specified\nLatest Backup: 2012-11-17"
I see.
You need to differentiate a literal backslash followed by a letter n as a sequence of two characters, and a LF character (a.k.a. newline) that is usually represented as \n.
You also need to distinguish two different kinds of quoting you're using in Ruby: singles and doubles. Single quotes are literal: the only thing that is interpreted in single quotes specially is the sequence \', to escape a single quote, and the sequence \\, which produces a single backslash. Thus, '\n' is a two-character string of a backslash and a letter n.
Double quotes allow for all kinds of weird things in it: you can use interpolation with #{}, and you can insert special characters by escape sequences: so "\n" is a string containing the LF control character.
Now, in your database you seem to have the former (backslash and n), as hinted by two pieces of evidence: the fact that you're seeing literal backslash and n when you print it, and the fact that gsub finds a '\n'. What you need to do is replace the useless backslash-and-n with the actual line separator characters.
#servicedata.service_exit_details.gsub('\n', "\r\n")

Comma, ')',or valid expression continuation expected

I need my VB.net to write a file containing the following line
objWriter.WriteLine ("TEXTA " (FILEA) " TEXTB")
Unfortunatly the variable (FILEA) is causing problems i now get the error
Comma, ')', or valid expression continuation expected.
Could someone explain this please?
You're not concatenating (joining) the strings proerly...
objWriter.WriteLine ("TEXTA " & FILEA & " TEXTB")
A better style to get into the habit of using is:
objWriter.WriteLine (string.format("TEXTA {0} TEXTB", FILEA))
The FILEA variable replaces the {0} placeholder in the format string. Depending on what the writer you're using is, you may have a formatted overload so you could just do:
objWriter.WriteLine ("TEXTA {0} TEXTB", FILEA)
And since you asked for an explanation;
The compiler is asking you what exactly you want it to do - you've given it 3 variables (String, variable, String) and haven't told it that you want to join them together - It's saying that after the first string "TEXTA", there should either be the closing bracket (to end the method call), a comma (to pass another parameter to the method) OR a "valid continuation expression" - ie something that tells it what to do with the next bit. in this case, you want a continuation expression, specifically an ampersand to signify "concatenate with the next 'thing'".
Presumably you're looking for string concatenation? Try this:
objWriter.WriteLine("TEXTA" & FILEA & "TEXTB");
Note that FILEA isn't exactly a conventional variable name... which leads me to suspect there may be something else you're trying to achieve. Could you give more details?

How can I write special character in VB code

I have a Sql statament using special character (ex: ('), (/), (&)) and I don't know how to write them in my VB.NET code. Please help me. Thanks.
Find out the Unicode code point for the character (from http://www.unicode.org) and then use ChrW to convert from the code point to the character. (To put this in another string, use concatenation. I'm somewhat surprised that VB doesn't have an escape sequence, but there we go.)
For example, for the Euro sign (U+20AC) you'd write:
Dim euro as Char = ChrW(&H20AC)
The advantage of this over putting the character directly into source code is that your source code stays "just pure ASCII" - which means you won't have any strange issues with any other program trying to read it, diff it, etc. The disadvantage is that it's harder to see the symbol in the code, of course.
The most common way seems to be to append a character of the form Chr(34)... 34 represents a double quote character. The character codes can be found from the windows program "charmap"... just windows/Run... and type charmap
If you are passing strings to be processed as SQL statement try doubling the characters for example.
"SELECT * FROM MyRecords WHERE MyRecords.MyKeyField = ""With a "" Quote"" "
The '' double works with the other special characters as well.
The ' character can be doubled up to allow it into a string e.g
lSQLSTatement = "Select * from temp where name = 'fred''s'"
Will search for all records where name = fred's
Three points:
1) The example characters you've given are not special characters. They're directly available on your keyboard. Just press the corresponding key.
2) To type characters that don't have a corresponding key on the keyboard, use this:
Alt + (the ASCII code number of the special character)
For example, to type ¿, press Alt and key in 168, which is the ASCII code for that special character.
You can use this method to type a special character in practically any program not just a VB.Net text editor.
3) What you probably looking for is what is called 'escaping' characters in a string. In your SQL query string, just place a \ before each of those characters. That should do.
Chr() is probably the most popular.
ChrW() can be used if you want to generate unicode characters
The ControlChars class contains some special and 'invisible' characters, plus the quote - for example, ControlChars.Quote