Make Python string a raw string
The idea behind r' '
is to write raw string literals, because it changes the way python escape characters. If it happens to be a value from a variable, as you stated above, you don't need to use r' '
because you are not explicitly writing a string literal.
Either way, I think this will do:
path = r'%s' % pathToFile
EDIT:
Also, as commented to the question, you should really be sure the path exists.
casting raw strings python
Python 3:
"hurr..\n..durr".encode('unicode-escape').decode()
Python 2:
"hurr..\n..durr".encode('string-escape')
Converting string to raw string for json processing [Python]
You're going the wrong direction here, if you want to create JSON. You want dumps
, not loads
':
def createJsonText(txt):
x = {'text': txt}
y = json.dumps(x)
open('tone.json','w').write(y)
Your code had mode='a' for append, but a set of separate JSON lines is NOT a valid JSON file. If you want it to be a valid JSON file, you need the whole file to be one document.
Update
Alternatively:
def createJsonText(txt):
json.dump({'text':txt}, open('tone.json','w'))
How to create raw string from string variable in python?
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
Related Topics
How to Clear/Delete the Contents of a Tkinter Text Widget
Pandas - Find Rows With Matching Values in Two Columns and Multiply Value in Another Column
How to Make a Roll the Dice Command With My Discord Bot
Python - How to Pad the Output of a MySQL Table
Json.Decoder.Jsondecodeerror: Expecting Value: Line 1 Column 1 (Char 0) Python
Can Anyone Explain Me What This Python 3 Command Do
Check If a Key Exists in a Bucket in S3 Using Boto3
How to Plot Predicted Values VS the True Value
How to Sum Multiple Columns in a Spark Dataframe in Pyspark
Invalidargumenterror: Logits and Labels Must Have the Same First Dimension Seq2Seq Tensorflow
Pip Error: Microsoft Visual C++ 14.0 Is Required
Pandas Populate New Dataframe Column Based on Matching Columns in Another Dataframe
How to Run Two Python Scripts Simultaneously from a Master Script
Passing a List of Values from Python to the in Clause of an SQL Query
Python - Regex Match Multiple Patterns in Multiple Lines
How to Install Python Packages from the Tar.Gz File Without Using Pip Install