I want to generate C code with a Python script, and not have to escape things. For example, I have tried:
myFile.write(someString + r'\r\n\')
hoping that a r prefix would make things work. However, I'm still getting the error:
myFile.write(someString + ur'\r\n\')
^
SyntaxError: EOL while scanning string literal
How can I write raw strings to a file in Python?
Python raw stings can't end with a backslash.
However, there are workarounds.
You can just add a whitespace at the end of the string:
>>> with open("c:\\tmp\\test.txt", "w") as myFile:
... myFile.write(someString + r'\r\n\ ')
You propably don't bother with that, so that may be a solution.
Assume someString is Hallo.
This will write Hallo\r\n\_ to the file, where _ is a space.
If you don't like the extra space, you can remove it like this:
>>> with open("c:\\tmp\\test.txt", "w") as myFile:
... myFile.write(someString + r'\r\n\ '[:-1])
This will write Hallo\r\n\ to the file, without the extra whitespace, and without escaping the backslash.
You need to escape the last \ so it doesn't escape the end of string, but if you put it as part of a raw string, it won't get you exactly what you want:
>>> r'\r\n\\'
'\\r\\n\\\\'
Python's string literal concatenation, however, lets you mix raw and normal strings:
>>> r'\r\n' '\\'
'\\r\\n\\'
You could insert the raw string into the string via the format method. This ensures
that the raw string will be inserted with the proper escaping.
Example:
mystring = "some string content {0}"
# insert raw string
mystring = mystring.format(r"\r\n\\")
myfile = open("test.txt", "w")
myfile.write(mystring)
myfile.close()
myFile.write(someString + r'\r\n\\')
Just escape your strings ^^
There is no way to have a string literal of arbitrary contents without escaping. You will always run into problems, since there is no way of for example having the "end-of-literal character", in this case ' there without escaping, as Python would be unable to tell if it is the end of the string literal or part of it.
And that's the entire reason why we have escaping in the first place. So the answer to your question is: You can't. Not in Python nor any other language.
Related
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 3 years ago.
I'm trying to read a CSV file into Python (Spyder), but I keep getting an error. My code:
import csv
data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
data = csv.reader(data)
print(data)
I get the following error:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes
in position 2-3: truncated \UXXXXXXXX escape
I have tried to replace the \ with \\ or with / and I've tried to put an r before "C.., but all these things didn't work.
This error occurs, because you are using a normal string as a path. You can use one of the three following solutions to fix your problem:
1: Just put r before your normal string. It converts a normal string to a raw string:
pandas.read_csv(r"C:\Users\DeePak\Desktop\myac.csv")
2:
pandas.read_csv("C:/Users/DeePak/Desktop/myac.csv")
3:
pandas.read_csv("C:\\Users\\DeePak\\Desktop\\myac.csv")
The first backslash in your string is being interpreted as a special character. In fact, because it's followed by a "U", it's being interpreted as the start of a Unicode code point.
To fix this, you need to escape the backslashes in the string. The direct way to do this is by doubling the backslashes:
data = open("C:\\Users\\miche\\Documents\\school\\jaar2\\MIK\\2.6\\vektis_agb_zorgverlener")
If you don't want to escape backslashes in a string, and you don't have any need for escape codes or quotation marks in the string, you can instead use a "raw" string, using "r" just before it, like so:
data = open(r"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
You can just put r in front of the string with your actual path, which denotes a raw string. For example:
data = open(r"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
Consider it as a raw string. Just as a simple answer, add r before your Windows path.
import csv
data = open(r"C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
data = csv.reader(data)
print(data)
Try writing the file path as "C:\\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener" i.e with double backslash after the drive as opposed to "C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener"
Add r before your string. It converts a normal string to a raw string.
As per String literals:
String literals can be enclosed within single quotes (i.e. '...') or double quotes (i.e. "..."). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings).
The backslash character (i.e. \) is used to escape characters which otherwise will have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter r or R. Such strings are called raw strings and use different rules for backslash escape sequences.
In triple-quoted strings, unescaped newlines and quotes are allowed, except that the three unescaped quotes in a row terminate the string.
Unless an r or R prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C.
So ideally you need to replace the line:
data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
To any one of the following characters:
Using raw prefix and single quotes (i.e. '...'):
data = open(r'C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener')
Using double quotes (i.e. "...") and escaping backslash character (i.e. \):
data = open("C:\\Users\\miche\\Documents\\school\\jaar2\\MIK\\2.6\\vektis_agb_zorgverlener")
Using double quotes (i.e. "...") and forwardslash character (i.e. /):
data = open("C:/Users/miche/Documents/school/jaar2/MIK/2.6/vektis_agb_zorgverlener")
Just putting an r in front works well.
eg:
white = pd.read_csv(r"C:\Users\hydro\a.csv")
It worked for me by neutralizing the '' by f = open('F:\\file.csv')
The double \ should work for Windows, but you still need to take care of the folders you mention in your path. All of them (except the filename) must exist. Otherwise you will get an error.
Is there a way to declare a string variable in Python such that everything inside of it is automatically escaped, or has its literal character value?
I'm not asking how to escape the quotes with slashes, that's obvious. What I'm asking for is a general purpose way for making everything in a string literal so that I don't have to manually go through and escape everything for very large strings.
Raw string literals:
>>> r'abc\dev\t'
'abc\\dev\\t'
If you're dealing with very large strings, specifically multiline strings, be aware of the triple-quote syntax:
a = r"""This is a multiline string
with more than one line
in the source code."""
There is no such thing. It looks like you want something like "here documents" in Perl and the shells, but Python doesn't have that.
Using raw strings or multiline strings only means that there are fewer things to worry about. If you use a raw string then you still have to work around a terminal "\" and with any string solution you'll have to worry about the closing ", ', ''' or """ if it is included in your data.
That is, there's no way to have the string
' ''' """ " \
properly stored in any Python string literal without internal escaping of some sort.
You will find Python's string literal documentation here:
http://docs.python.org/tutorial/introduction.html#strings
and here:
http://docs.python.org/reference/lexical_analysis.html#literals
The simplest example would be using the 'r' prefix:
ss = r'Hello\nWorld'
print(ss)
Hello\nWorld
(Assuming you are not required to input the string from directly within Python code)
to get around the Issue Andrew Dalke pointed out, simply type the literal string into a text file and then use this;
input_ = '/directory_of_text_file/your_text_file.txt'
input_open = open(input_,'r+')
input_string = input_open.read()
print input_string
This will print the literal text of whatever is in the text file, even if it is;
' ''' """ “ \
Not fun or optimal, but can be useful, especially if you have 3 pages of code that would’ve needed character escaping.
Use print and repr:
>>> s = '\tgherkin\n'
>>> s
'\tgherkin\n'
>>> print(s)
gherkin
>>> repr(s)
"'\\tgherkin\\n'"
# print(repr(..)) gets literal
>>> print(repr(s))
'\tgherkin\n'
>>> repr('\tgherkin\n')
"'\\tgherkin\\n'"
>>> print('\tgherkin\n')
gherkin
>>> print(repr('\tgherkin\n'))
'\tgherkin\n'
Update for clarification
I have to replicate the functionality from a server. One of the responses of this old server is the one seen here http://test.muchticket.com/api/?token=carlos&method=ventas&ESP=11, except that the double slashes should be single ones.
End of update
Update No.2 for clarification
This variable then goes to a dictionary wich is dumped to an HttpResponse with this
return HttpResponse(json.dumps(response_data,sort_keys=True), content_type="application/json")
I hate my job.
End of update
I need to store 'http:\/\/shop.muchticket.com\/' in a variable. And then save it in a dictionary. I have tried several different methods, but none of them seems to work, here are some examples of what I've tried:
url = 'http:\/\/shop.muchticket.com\/'
print url
>> http:\\/\\/shop.muchticket.com\\/
With raw
url = r'http:\/\/shop.muchticket.com\/'
print url
>> http:\\/\\/shop.muchticket.com\\/
With the escape character
url = 'http:\\/\\/shop.muchticket.com\\/'
print url
>> http:\\/\\/shop.muchticket.com\\/
Raw and escape character
url = r'http:\\/\\/shop.muchticket.com\\/'
print url
>> http:\\\\/\\\\/shop.muchticket.com\\\\/
Escape character and decode
url = 'http:\\/\\/shop.muchticket.com\\/'
print url.decode('string_escape')
>> http:\\/\\/shop.muchticket.com\\/
Decode only
url = 'http:\/\/shop.muchticket.com\/'
print url.decode('string_escape')
>> http:\\/\\/shop.muchticket.com\\/
The best way is not to use any escape sequences
>>> s = 'http://shop.muchticket.com/'
>>> s
'http://shop.muchticket.com/'
>>> print(s)
http://shop.muchticket.com/
Unlike "other" languages, you do not need to escape the forward slash (/) in Python!
If you need the forward slash then
>>> s = 'http:\/\/shop.muchticket.com\/'
>>> print(s)
http:\/\/shop.muchticket.com\/
Note: When you just type s in interpreter it gives you the repr output and thus you get the escaped forward slash
>>> s
'http:\\/\\/shop.muchticket.com\\/' # Internally stored!!!
>>> print(repr(s))
'http:\\/\\/shop.muchticket.com\\/'
Therefore Having a single \ is enough to store it in a variable.
As J F S says,
To avoid ambiguity, either use raw string literals or escape the
backslashes if you want a literal backslash in the string.
Thus your string would be
s = 'http:\\/\\/shop.muchticket.com\\/' # Escape the \ literal
s = r'http:\/\/shop.muchticket.com\/' # Make it a raw string
If you need two characters in the string: the backslash (REVERSE SOLIDUS) and the forward slash (SOLIDUS) then all three Python string literals produce the same string object:
>>> '\/' == r'\/' == '\\/' == '\x5c\x2f'
True
>>> len(r'\/') == 2
True
The preferable way to write it is: r'\/' or '\\/'.
The reason is that the backslash is a special character in a string literal (something that you write in Python source code (usually by hand)) if it is followed by certain characters e.g., '\n' is a single character (newline) and '\\' is also a single character (the backslash). But '\/' is not an escape sequence and therefore it is two characters. To avoid ambiguity, use raw string literals r'\/' where the backslash has no special meaning.
The REPL calls repr on a string to print it:
>>> r'\/'
'\\/'
>>> print r'\/'
\/
>>> print repr(r'\/')
'\\/'
repr() shows your the Python string literal (how you would write it in a Python source code). '\\/' is a two character string, not three. Don't confuse a string literal that is used to create a string and the string object itself.
And to test the understanding:
>>> repr(r'\/')
"'\\\\/'"
It shows the representation of the representation of the string.
For Python 2.7.9, ran:
url = "http:\/\/shop.muchticket.com\/"
print url
With the result of:
>> http:\/\/shop.muchticket.com\/
What is the version of Python you are using? From Bhargav Rao's answer, it seems that it should work in Python 3.X as well, so maybe it's a case of some weird imports?
I am trying to create a new text file in an empty folder. The path to the folder is:
C:\Users\Tor\Desktop\Python files\retning
When I type this in the command line in windows explorer, I get straight to the empty folder.
When I type my code in Python I get an errormessage and it looks like Python has replaced a couple of the '\' with '\\'
This is my code
sector='A5'
g=open('C:\Users\Tor\Desktop\Python files\retning\retning'+sector+'.txt', 'a')
and this is the errormessage
Traceback (most recent call last):
File "C:\Users\Tor\Desktop\Python files\filer som behandler output\Vindretning.py", line 2, in <module>
g=open('C:\Users\Tor\Desktop\Python files\retning\retning'+sector+'.txt', 'a')
IOError: [Errno 22] invalid mode ('a') or filename: 'C:\\Users\\Tor\\Desktop\\Python files\retning\retningA5.txt'
Can anyone please tell me what I am doing wrong, or what is happening here?
\ needs to be escaped in the strings. That is why \\ or raw strings are used (r'test String')
Using raw strings solves the problem here. Something like,
open(r'C:\Programming Test Folder\test_file.py')
So, your code gets changed to
g=open(r'C:\Users\Tor\Desktop\Python files\retning\retning{}.txt'.format(sector), 'a')
Or use / in Windows, like follows
g=open('C:/Users/Tor/Desktop/Python files/retning/retning'+sector+'.txt', 'a')
This is normal behaviour; Python is giving you a string representation that can be pasted right back into a Python script or interpreter prompt. Since \ is a character used in Python string literals to start an escape sequence (such as \n or \xa0) the backslashes are doubled.
In fact, it is the characters without escaped backslashes that are the key here; \r is the escape code for a carriage return. You need to use one of the following options to specify Windows paths instead:
Escape all backslashes by doubling them in your string literals:
g = open('C:\\Users\\Tor\\Desktop\\Python files\\retning\\retning'+sector+'.txt', 'a')
Now the \r won't be interpreted as an escape code.
Use a raw string literal:
g = open(r'C:\Users\Tor\Desktop\Python files\retning\retning'+sector+'.txt', 'a')
In raw string literals most escape codes are ignored.
Use forward slashes:
g = open('C:/Users/Tor/Desktop/Python files/retning/retning'+sector+'.txt', 'a')
Forward slashes work fine as path separators on Windows, and there's no chance of them being interpreted as escape characters.
In a normal python string, a backslash can have a special meaning (for instance, \n indicates a new line). In the path you've provided in your code, you either need to use \\ for each directory separator (\\ means include a ), or mark the string as a raw string, meaning the special treatment for backslashes doesn't apply. You do that with an r before the quote mark, like r'Folder\Sub-Folder\Another'
The error message is basically python giving you the python code you can use to get your original string.
I want the newline \n to show up explicitly when printing a string retrieved from elsewhere. So if the string is 'abc\ndef' I don't want this to happen:
>>> print(line)
abc
def
but instead this:
>>> print(line)
abc\ndef
Is there a way to modify print, or modify the argument, or maybe another function entirely, to accomplish this?
Just encode it with the 'string_escape' codec.
>>> print "foo\nbar".encode('string_escape')
foo\nbar
In python3, 'string_escape' has become unicode_escape. Additionally, we need to be a little more careful about bytes/unicode so it involves a decoding after the encoding:
>>> print("foo\nbar".encode("unicode_escape").decode("utf-8"))
unicode_escape reference
Another way that you can stop python using escape characters is to use a raw string like this:
>>> print(r"abc\ndef")
abc\ndef
or
>>> string = "abc\ndef"
>>> print (repr(string))
>>> 'abc\ndef'
the only proplem with using repr() is that it puts your string in single quotes, it can be handy if you want to use a quote
Simplest method:
str_object.replace("\n", "\\n")
The other methods are better if you want to show all escape characters, but if all you care about is newlines, just use a direct replace.