The raw string notation is only used in Python source code; all strings declared as raw strings are "converted" to normal strings with the necessary escape sequences added during "compile time" (unlike (in Python 2) the two different string types string/Unicode string):
>>> r"\b"
'\\b'
>>> "Hello"
'Hello'
>>> u"Hello"
u'Hello'
If you read the string from a file, it will already be correctly escaped.
(Assuming test.txt contains (?i:\bsys\.user_catalog\b)):
f = open("test.txt").read()
print f
print repr(f)
Output:
(?i:\bsys\.user_catalog\b)
'(?i:\\bsys\\.user_catalog\\b)'
Answer from Tim Pietzcker on Stack OverflowThe raw string notation is only used in Python source code; all strings declared as raw strings are "converted" to normal strings with the necessary escape sequences added during "compile time" (unlike (in Python 2) the two different string types string/Unicode string):
>>> r"\b"
'\\b'
>>> "Hello"
'Hello'
>>> u"Hello"
u'Hello'
If you read the string from a file, it will already be correctly escaped.
(Assuming test.txt contains (?i:\bsys\.user_catalog\b)):
f = open("test.txt").read()
print f
print repr(f)
Output:
(?i:\bsys\.user_catalog\b)
'(?i:\\bsys\\.user_catalog\\b)'
You can use raw string anywhere you are using a string. Raw string is just a user friendly way to represent a string when you have lots of escape characters.
The second case is not working because of the '\'. So you need to escape it using another '\'. The second case should work if you give '(?i:\\bsys\\.user_catalog\\b)'. In memory, since ASCII or Unicode is stored, it doesn't make any difference if it is raw string or not.
Save strings as raw string to txt file
binaryfiles - How to open a binary file as raw string in python? - Stack Overflow
Python how to read raw binary from a file? (audio/video/text) - Stack Overflow
How to create raw string from string variable in python? - Stack Overflow
I am trying to import documents of multi-paragraph text into Microsoft Access, after processing them in Python. Unfortunately, Access seem to think each sentence is a new record, despite setting "^" as the delimiter.
I want to instead write my strings in the raw string format to work around this problem.
So I want:
y = '''
Hello, how are
you
'''
to be saved as '\nHello, how are\n you\n' in a txt file.
I cannot find anything on how to convert or encode strings to raw strings. How do I go about doing this?
And also, I want to be able to read '\nHello, how are\n you\n' from the text file and convert it back to a normal Python string. How do I go about doing this?
After you have opened a file, that file reference has a read() method on it which takes the number of bytes that you want to read in.
with open("image.bin", "rb") as b_f:
OverScan = 0
sizeY = 480
reg = OverScan + 10
binary_data = b_f.read(2*192*(1+sizeY)*reg)
binary_data will now be of type bytes and hold the number of bytes that you have asked for
If you wish to read the entire file into memory, simply calling the file's read() method (with no arguments) will do.
Eg:
s = open("image.bin", "rb").read()
If you only wish to read up to a specific number of bytes (as in @Eric Dill's example), that can be passed as a parameter to the read method:
s = open("image.bin", "rb").read(SOME_NUMBER_OF_BYTES)
What you are reading IS really the "raw binary" content of your "binary" file. Strange as it might seems, binary data are not "0's and 1's" but binary words (aka bytes, cf http://en.wikipedia.org/wiki/Byte) which have an integer (base 10) value and can be interpreted as ascii chars. Or as integers (which is how one usually do binary operations). Or as hexadecimal. For what it's worth, "text" is actually "raw binary data" too.
To get a "binary" representation you can have a look here : Convert binary to ASCII and vice versa but that's not going to give you more "raw binary data" than what you actually have...
Now the question: why do you want these data as "0's and 1's" exactly ?
to get the binary representation I think you will need to import binascii, then:
byte = f.read(1)
binary_string = bin(int(binascii.hexlify(byte), 16))[2:].zfill(8)
or, broken down:
import binascii
filePath = "mysong.mp3"
file = open(filePath, "rb")
with file:
byte = file.read(1)
hexadecimal = binascii.hexlify(byte)
decimal = int(hexadecimal, 16)
binary = bin(decimal)[2:].zfill(8)
print("hex: %s, decimal: %s, binary: %s" % (hexadecimal, decimal, binary))
will output:
hex: 64, decimal: 100, binary: 01100100
There is no such thing as "raw string" once the string is created in the process. The "" and r"" ways of specifying the string exist only in the source code itself.
That means "\x01" will create a string consisting of one byte 0x01, but r"\x01" will create a string consisting of 4 bytes '0x5c', '0x78', '0x30', '0x31'. (assuming we're talking about python 2 and ignoring encodings for a while).
You mentioned in the comment that you're taking the string from the user (either gui or console input will work the same here) - in that case string character escapes will not be processed, so there's nothing you have to do about it. You can check it easily like this (or whatever the windows equivalent is, I only speak *nix):
% cat > test <<EOF
heredoc> \x41
heredoc> EOF
% < test python -c "import sys; print sys.stdin.read()"
\x41
My solution to convert string to raw string (works with this sequences only: '\a', \b', '\f', '\n', '\r', '\t', '\v' . List of all escape sequences is here):
def str_to_raw(s):
raw_map = {8:r'\b', 7:r'\a', 12:r'\f', 10:r'\n', 13:r'\r', 9:r'\t', 11:r'\v'}
return r''.join(i if ord(i) > 32 else raw_map.get(ord(i), i) for i in s)
Demo:
>>> file_path = "C:\Users\b_zz\Desktop\fy_file"
>>> file_path
'C:\\Users\x08_zz\\Desktop\x0cy_file'
>>> str_to_raw(file_path)
'C:\\Users\\b_zz\\Desktop\\fy_file'
Reading them into a list is trivially done with readlines():
f = open('your-file.dat')
yourList = f.readlines()
If you need the newlines stripped out you can use ars' method, or do:
yourList = [line.rstrip('\n') for line in f]
If you want a dictionary with keys from 1 to the length of the list, the first way that comes to mind is to make the list as above and then do:
yourDict = dict(zip(xrange(1, len(yourList)+1), yourList))
You can use the file with with like this. This is called a context manager and automatically closes the file at the end of the indented block
with open('data.txt') as f:
words = f.readlines()
If you want to do it without a context manager, you should close the file yourself
f = open('data.txt')
words = f.readlines()
f.close()
Otherwise the file remains open at least as long as f is still in scope
So I know that if you put r in front of a string python will make it a raw string.
However, python throws an error when I end a string with a \ such as in a file directory.
Example in question is r'C:\PycharmProjects\pythonProject2\files\'. This ends up giving me an error of SyntaxError: EOL while scanning string literal.
This is fixed if I add a second ' to the end of the file directory so that it reads r'C:\PycharmProjects\pythonProject2\files\'', but now if I want to create a file in this directory its created as 'file_name.txt as opposed to file_name.txt.
I'm using pycharm as my compiler if that makes any difference.