Replace two backslashes with a single backslash - python-3.x

I want to replace a string with two backslashes with single backslashes. However replace doesn't seem to accept '\\' as the replacement string. Here's the interpreter output:
>>> import tempfile
>>> temp_folder = tempfile.gettempdir()
>>> temp_folder
'C:\\Users\\User\\AppData\\Local\\Temp'
>>> temp_folder.replace('\\\\', '\\')
'C:\\Users\\User\\AppData\\Local\\Temp'
BTW, I know that Windows paths need to contain either double backslashes or a single forward slashes. I want to replace them anyway for display purposes.

Your output doesn't have double backslashes. What you are looking at is the repr() value of the string and that displays with escaped backslashes. Assuming your temp_folder would have double backslashes, you should instead use:
print(temp_folder.replace('\\\\', '\\'))
and that will show you:
C:\Users\User\AppData\Local\Temp
which also drops the quotes.
But your temp_folder is unlikely to have double backslashes and this difference in display probably got you thinking that there are double backslashes in the return value from tempfile.gettempdir(). As #Jean-Francois indicated, there should not be (at least not on Windows). So you don't need to use the .replace(), just print:
print(temp_folder)

This works for me
text = input('insert text')
list = text.split('\\')
print(list)
text2 = ''
for items in list:
if items != '':
text += items + '\\'
print(text2)

Related

Is it possible to use a "plain" long string?

In Julia, you can't store a string like that:
str = "\mwe"
Because there is a backslash. So the following allows you to prevent that:
str = "\\mwe"
The same occurs for "$, \n" and many other symbols. My question is, given that you have a extremely long string of thousands of characters and this is not very convenient to treat all the different cases even with a search and replace (Ctrl+H), is there a way to assign it directly to a variable?
Maybe the following (which I tried) gives an idea of what I'd like:
str = """\$$$ \\\nn\nn\m this is a very long and complicated (\n^$" string"""
Here """ is not suitable, what should I use instead?
Quick answer: raw string literals like raw"\$$$ \\\nn..." will get you most of the way there.
Raw string literals allow you to put nearly anything you like between quotes and Julia will keep the characters as typed with no replacements, expansions, or interpolations. That means you can do this sort of thing easily:
a = raw"\mwe"
#assert codepoint(a[1]) == 0x5c # Unicode point for backslash
b = raw"$(a)"
#assert codepoint(b[1]) == 0x25 # Unicode point for dollar symbol
The problem is always the delimiters that define where the string begins and ends. You have to have some way of telling Julia what is included in the string literal and what is not, and Julia uses double inverted commas to do that, meaning if you want double inverted commas in your string literal, you still have to escape those:
c = raw"\"quote" # note the backslashe
#assert codepoint(c[1]) == 0x22 # Unicode point for double quote marks
If this bothers you, you can combine triple quotes with raw, but then if you want to represent literal triple quotes in your string, you still have to escape those:
d = raw""""quote""" # the three quotes at the beginning and three at the end delimit the string, the fourth is read literally
#assert codepoint(d[1]) == 0x22 # Unicode point for double quote marks
e = raw"""\"\"\"""" # In triple quoted strings, you do not need to escape the backslash
#assert codeunits(e) == [0x22, 0x22, 0x22] # Three Unicode double quote marks
If this bothers you, you can try to write a macro that avoids these limitations, but you will always end up having to tell Julia where you want to start processing a string literal and where you want to end processing a string literal, so you will always have to choose some way to delimit the string literal from the rest of the code and escape that delimiter within the string.
Edit: You don't need to escape backslashes in raw string literals in order to include quotation marks in the string, you just need to escape the quotes. But if you want a literal backslash followed by a literal quotation mark, you have to escape both:
f = raw"\"quote"
#assert codepoint(f[1]) == 0x22 # double quote marks
g = raw"\\\"quote" # note the three backslashes
#assert codepoint(g[1]) == 0x5c # backslash
#assert codepoint(g[2]) == 0x22 # double quote marks
If you escape the backslash and not the quote marks, Julia will get confused:
h = raw"\\"quote"
# ERROR: syntax: cannot juxtapose string literal
This is explained in the caveat in the documentation.

How do I add the ' symbol to a string

I had someone help with a prior question to turn hexadecimal to string, but I want the string to output surrounded with '
so it returns 'I0KB' instead of just I0KB.
What I have:
with open('D:\new 4.txt', 'w') as f:
f.write('if not (GetItemTypeId(GetSoldItem())==$49304B42) then\n')
def hex_match_to_string(m):
return ''.join([chr(int(m.group(1)[i:i+2], 16)) for i in range(0, len(m.group(1)), 2)])
# ...
line = re.sub(r'\$((?:\w\w\w\w\w\w\w\w)+)', hex_match_to_string, line)
file_out.write(line)
output:
if not (GetItemTypeId(GetSoldItem())==I0KB) then
but I want it to output
if not (GetItemTypeId(GetSoldItem())=='I0KB') then
and using
def hex_match_to_string(m):
return ''.join(',[chr(int(m.group(1)[i:i+2], 16)) for i in range(0, len(m.group(1)), 2)],')
...gives me a syntax error even though I read that join(a,b,c) is the way to combine strings.
Thanks in advance for the help, and sorry I am clueless for what should be an easy task.
You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.
You should not add the quotes to the argument passed to join, but wrap the result of the join with quotes:
return "'" + ''.join([chr(int(m.group(1)[i:i+2], 16)) for i in range(0, len(m.group(1)), 2)]) + "'"
I think it's important to distinguish between enclosing a string between, single, double, or triple quotation marks. See answers here regarding the most common use of the third (the so-called doc-strings).
While most of the time you can use " and ' interchangeably, you can use them together in order to escape the quotation:
>>> print("''")
''
>>> print('"')
"
You can also use the double quotes three times to escape any double quotes in between:
>>> print(""" " " "j""")
" " "j
But I'd suggest against the last option, because it doesn't always work as expected, for example, print(""""""") will throw an error. (And of course, you could always use the \ to escape any special character.)

why does 2 back slashes appear in python if I enter a back slash followed by a special character

I am new to python. So just out of curiosity, I wrote the below string in IDLE:
'Happy Birth\^day' and I got the output as 'Happy Birth\\^day'
From where does python add an extra backslash?
The extra backslash is not actually added; it's just added by the repr() function to indicate that it's a literal backslash. The Python interpreter uses the repr() function (which calls __repr__() on the object) when the result of an expression needs to be printed:
>>> '\\'
'\\'
>>> print '\\'
\
>>> print '\\'.__repr__()
'\\'
So try -
print('Happy Birth\^day')
#Happy Birth\^day
And it will only print a single backslash.
That's just because of the representation and the escaping feature of \. It doesn't actually include double backslash. It adds it because of the future processes that may be done on the string.
Let't check it using len function in python to check whether the second backslash exists or not:
happy_birthday = 'Happy Birth\^day'
len(happy_birthday)
>>> 16
happy_birthday
>>> 'Happy Birth\\^day' # length is 17!
print(happy_birthday)
>>> 'Happy Birth\^day'
Result
As you see the len is considering only one backslash.
So actually it is not adding it. It is the representation mode which shows it like this and it will be correct when you try to use it.

Removing special characters from a string In a Groovy Script

I am looking to remove special characters from a string using groovy, i'm nearly there but it is removing the white spaces that are already in place which I want to keep. I only want to remove the special characters (and not leave a whitespace). I am running the below on a PostCode L&65$$ OBH
def removespecialpostcodce = PostCode.replaceAll("[^a-zA-Z0-9]+","")
log.info removespecialpostcodce
Currently it returns L65OBH but I am looking for it to return L65 OBH
Can anyone help?
Use below code :
PostCode.replaceAll("[^a-zA-Z0-9 ]+","")
instead of
PostCode.replaceAll("[^a-zA-Z0-9]+","")
To remove all special characters in a String you can use the invert regex character:
String str = "..\\.-._./-^+* ".replaceAll("[^A-Za-z0-1]","");
System.out.println("str: <"+str+">");
output:
str: <>
to keep the spaces in the text add a space in the character set
String str = "..\\.-._./-^+* ".replaceAll("[^A-Za-z0-1 ]","");
System.out.println("str: <"+str+">");
output:
str: < >

Python - exclude data with double quotes and parenthesis

I have a list which contains set of words in single quotes and double quotes, now i want to grep all the data only in single quotes.
Input_
sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
Output:
telco_name_list.event_date,reference.date,data_flow.code
I want to exclude the parenthesis and string in double quotes.
Since you tagged the post as python-3.x, I'm assuming you want to write python code. Based on your example this would work:
#!/usr/bin/env python3
Input_sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]
comma = False
result = []
for i in range(len(Input_sentence)):
e = Input_sentence[i]
if e.startswith("'"): continue
e = e.replace('(', '').replace(')', '')
if comma:
print(",", end="")
comma = True
print(e, end="")
print()
This code iterates through the list, ignoring elements which begin with a single quote and filtering out parentheses from elements before printing them on stdout. This code works for the given example, but may or may not be what you need as the exact semantics of what you want out are somewhat ambiguous (i.e. is it fine to filter out all parentheses or just the ones at the beginning and end of your elements?).

Resources