\\x to \x with python [duplicate] - python

This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed last month.
I have a string like this:
'\\xac\\x85'
and I want to encode these string like this another:
'\xac\x85'
I tried a lot of things like encode tu utf-8, replace \\x, etc. But when I print the result it always is with \\x.
Any idea?

'\\xac\\x85' is the literal representation of '\xac\x85'. So you dont have to do any translation
>>> print ('\\xac\\x85')
\xac\x85

I made this function when I had the same issue
I included a little demo! Hope this helps people!
def FixSlash(string):
strarr = string.split("\\x");
string = strarr[0]
for i in strarr[1:]:
string+=chr(int(i[:2],16))+''.join(i[2:])
return string```
my_string = FixSlash("my string has '\\xac\\x85' in it")
print(my_string) # returns "my string has '¬' in it" \x85 acts like a new line
print(my_string.encode("latin1")) # returns b"my string has '\xac\x85' in it" proving the '\\x' to '\x' was successful!
#TIP: never trust UTF-8 for byte encoding!!

Related

How do I print the characters "\n" instead of whitespace using python and C? [duplicate]

This question already has answers here:
In Python, is it possible to escape newline characters when printing a string?
(3 answers)
Closed 1 year ago.
I want to print the specific characters \n in python and C language. Of course its the newline character so every time I say print("\n") or printf("\n") it simply prints whitespace.
What I want are the specific characters \ and n as the output.
Could someone please help with this?
In C you need to add another "\" to your code like this.
#include <stdio.h>
main() {
printf("\\n");
}
For Python you can use repr
>>> string = "\n"
>>> print(repr(string))
'\n'
You can use repr() to represent a raw string.
print(repr('\n'))

Can't replace a string with multiple escape characters

I am having trouble with the replace() method. I want to replace some part of a string, and the part which I want to replace consist of multiple escape characters. It looks like something like this;
['<div class=\"content\">rn
To remove it, I have a block of code;
garbage_text = "[\'<div class=\\\"content\\\">rn "
entry = entry.replace(garbage_text,"")
However, it does not work. Anything is removed from my complete string. Can anybody point out where exactly I am thinking wrong about it? Thanks in advance.
Addition:
The complete string looks like this;
"['<div class=\"content\">rn gitar calmak icin kullanilan minik plastik garip nesne.rn </div>']"
You could use the triple quote format for your replacement string so that you don't have to bother with escaping at all:
garbage_text = """['<div class="content">rn """
Perhaps your 'entry' is not formatted correctly?
With an extra variable 'text', the following worked in Python 3.6.7:
>>> garbage_text
'[\'<div class=\\\'content\'\\">rn '
>>> text
'[\'<div class=\\\'content\'\\">rn And then there were none'
>>> entry = text.replace(garbage_text, "")
>>> entry
'And then there were none'

How to write a string starting with ' and ending with " in Python? [duplicate]

This question already has answers here:
Having both single and double quotation in a Python string
(9 answers)
Closed 5 years ago.
I'd like to save the following characters 'bar" as a string variable, but it seems to be more complicated than I thought :
foo = 'bar" is not a valid string.
foo = ''bar"' is not a valid string either.
foo = '''bar"'' is still not valid.
foo = ''''bar"''' actually saves '\'bar"'
What is the proper syntax in this case?
The last string saves '\'bar"' as the representation, but it is the string you're looking for, just print it:
foo = ''''bar"'''
print(foo)
'bar"
when you hit enter in the interactive interpreter you'll get it's repr which escapes the second ' to create the string.
Using a triple quoted literal is the only way to define this without explicitly using escapes. You can get the same result by escaping quotes:
print('\'foo"')
'foo"
print("'foo\"")
'foo"

Python: os.join.path() [duplicate]

This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 6 years ago.
I used os.join.path() to load image in a folder. But I found the function cannot give accurate path when it is used in defining another function in some cases. For example:
def Myfuncion(something)
desiredPath = os.path.join('myPath','apple.jpeg')
#desiredPath = os.path.normpath(os.path.join('myPath','apple.jpeg'))
print desiredPath
return
When I implement the function, the printed result of the path is:
myPath\apple.jpeg
It is illegal for image loading. But os.path.join() works well in Pythonconsole.
How to make the path generated in such function definition have double backslashes?
Also, it is noted os.path.normpath also cannot work well sometimes. For example:
os.path.normpath('myPath\apple')
It should give the result:
myPath\\apple
But instead, it results in:
'myPath\x07pple'
How come??
\a is equivalent to \x07. (See escape sequence part in String and bytes literals.)
>>> '\a'
'\x07'
You need to escape \ to mean backslash literally:
>>> '\\a'
'\\a'
>>> print('\\a')
\a
or, use raw string literal:
>>> r'\a'
'\\a'
>>> print(r'\a')
\a

Why does python use both " " and ' ' to make a string literal [duplicate]

This question already has answers here:
Single quotes vs. double quotes in Python [closed]
(19 answers)
Closed 9 years ago.
Python's Philosophy:
....
Simple is better than complex.
....
So why does it use both double quote " " and single quote ' ' to indicate something is a string literal.
I was lost when I typed:
x = "aa"
x
And saw:
'aa'
But not:
"aa"
When I saw the above philosophy, I doubted it. It needs some explanations.
This isn't complex at all. In fact, it's rather helpful.
Both '' and "" can be used in python. There's no difference, it's up to you.
In your example, when you type x, you are given the representation of the string (i.e, it's equivalent to print repr(x)). It wouldn't have mattered if you did x = 'aa'
I like to use either for cases such as:
print 'Bill said, "Hey!"'
print "I'm coming!"
If we used " for the first example, then there would be an error because Python would interpret it as "Bill said, ".
If we used ' for the second example, then there would be an error because Python would interpret it as 'I'
Of course, you could just escape the apostrophes, but beautiful is better than ugly.
You can use "" to be able to write "I'm legend" for example.

Categories

Resources