Python String cleanup from spaces and Unknown empty element - python-3.x

I got attribute from Selenium Element and it contains empty char or spaces:
When I double click the result :
In VS code:
What I tried so far :
string.replace(" ","") #didnt work
So I came with this resolution (I know its bad ):
edit1 = ticketID[:1]
ticketF = ticketID.replace(edit1,"")
edit2 = ticketF[:1]
ticketE = ticketF.replace(edit2,"")
edit3 = ticketE[:1]
ticketD = ticketE.replace(edit3,"")
What Im looking for is what is those blanks ? tabs ? new lines ?
how to make it better ?
Edit:
ticketID.replace("\n","")
ticketID.replace(" ","")
ticketID.strip()

Those are basically whitespaces, Please use .strip() for any trailing spaces.
In Python, the stripping methods are capable of removing leading and trailing spaces and specific characters. The leading and trailing spaces include blanks, tabs (\t), carriage returns (\r, \n), and the other lesser-known whitespace characters.
If you have ele as an web element.
You can use .text to get the text and then on top of that use .strip()
Probably in your code :
ticketID.text.strip()

They look like lines and not spaces to me.
string.replace("\n","")

Related

Replacing "invisible" special characters with something legible

About twelve years ago, I wrote a small VB.NET application that loads strings from files. These strings may contain one or more of the following characters: à, è, é, ì, ò, ù, ä, ö. The application uses a special custom font (JazzText Extended) that does not have those special characters. Yet, I somehow managed to make the application display words correctly in that font, and twelve years later, I have no idea how - thanks for not leaving a line of comment, past me!
The program has the following routine:
Private Sub SetWord(ByVal word() As String)
Dim nword(3) As String
nword(0) = word(0)
nword(1) = word(1)
nword(2) = word(2)
For i As Integer = 0 To 2
nword(i) = nword(i).Replace("à", "")
nword(i) = nword(i).Replace("é", "")
nword(i) = nword(i).Replace("è", "")
nword(i) = nword(i).Replace("ì", "ê")
nword(i) = nword(i).Replace("ò", "")
nword(i) = nword(i).Replace("ù", "")
nword(i) = nword(i).Replace("ä", "")
nword(i) = nword(i).Replace("ö", "")
Next
lblItaWord.Text = nword(0).ToUpper
lblEngWord.Text = nword(1).ToUpper
lblFinWord.Text = nword(2).ToUpper
End Sub
What it does is, it takes an array that contains three words, and for each of those three words, it looks if it contains any of the special characters. If it does, it replaces them with... something, makes the words all caps, and then assigns each of them to one of three labels.
In Visual Studio, the replacement characters look like empty strings. I had to put the cursor in between the quotation marks to realise that it was in fact not an empty string and there was an invisible character there. Here on SO... I'm not sure what you'll see. You might see just a square, or some other weird character. (The ê character is an exception, it seems to display in the same way everywhere.)
If you copypaste any of the invisible/square characters to Google and search for it, you'll get a different representation that uses two characters—for example, the first one translates to ‡. Using this pair in place of the invisible/square character in the Replace method does not produce the correct result. FYI, the encoding I use to read the files (the default one used by IO.StreamReader if you don't specify any encoding) works fine: if I use a more standard font, all special characters display correctly without using the SetWord sub at all.
Now, I have absolutely no idea how those characters, whatever they may be, manage to make the app display correctly the words when the font I use does not have those characters. I have no idea how I found out about this trick, either. Right now, my problem is that I would like to replace those squares/invisible characters with something intelligible, and I have no idea how. Any ideas?

Remove characters from string (DataFrame)

How do I remove extra characters with REGEX in this string code snippet below.
From This : Fulham\n3.20\nDraw\n3.25\nSouthampton\n2.25\n
To Desired Outcome: 3.20\n\n3.25\n\n2.25
Note: I've tried with this regex -> ([^\d.\n]) but it leaves unwanted 'n' in team name if applicable.
([^\d\.\\n])
Fulham\n3.20\nDraw\n3.25\nSouthampton\n2.25\n
Try this:
s = "Fulham\n3.20\nDraw\n3.25\nSouthampton\n2.25\n"
"\n\n".join(i for i in s.split() if re.search(r"\d", i))
Output:
'3.20\n\n3.25\n\n2.25'
You can also use str.replace.
df['column_name'].str.replace(r'[a-zA-Z]','')
If you don't need the trailing and leading \n you can then use strip('\n')

excel trim function is removing spaces in middle of text - this was unexpected (?)

The excel trim function is removing spaces in middle of text - this was unexpected (?)
i.e. I thought that the excel trim was for trimming leading and trailing spaces.
e.g. a cell value of =Trim("Last Obs Resp") becomes a value of "Last Obs Resp"
Sure enough Microsoft documents it this way:
https://support.office.com/en-gb/article/trim-function-410388fa-c5df-49c6-b16c-9e5630b479f9
I am used to the Oracle database trim function which only removes leading and trailing spaces.
https://www.techonthenet.com/oracle/functions/trim.php
Was excel Trim function always this way?
Excel does not have ltrim and rtrim functions..
i.e. I can't do:
=RTRIM(Ltrim("Last Obs Resp"))
I wonder how I achieve the equivalent in Excel when I don't want to remove doubled up spaces in the middle of the string?
This page documents VBA trim function:
https://www.techonthenet.com/excel/formulas/trim.php
Create a UDF that uses VBA's version of Trim which does not touch the inner spaces. Only removing the leading and trailing spaces
Function MyTrim(str As String) As String
MyTrim = Trim(str)
End Function
Then you can call it from the worksheet:
=MyTrim(A1)
If you want a formula to do it:
=MID(LEFT(A1,AGGREGATE(14,6,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1)))/(MID(A1,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1))),1)<>" "),1)),AGGREGATE(15,6,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1)))/(MID(A1,ROW($XFD$1:INDEX(XFD:XFD,LEN(A1))),1)<>" "),1),999)

How to replace part of a string with an added condition

The problem:
The objective is to convert: "tan(x)*arctan(x)"
Into: "np.tan(x)*np.arctan(x)"
What I've tried:
s = "tan(x)*arctan(x)"
s = s.replace('tan','np.tan')
Out: np.tan(x)*arcnp.tan(x)
However, using pythons replace method resulted in arcnp.tan.
Taking one additional step:
s = s.replace('arcnp.', 'np.arc')
Out: np.tan(x)*np.arctan(x)
Achieves the desired result... but this solution is sloppy and inefficient.
Is there a more efficient solution to this problem?
Any help is appreciated. Thanks in advance.
Here is a way to do the job:
var string = 'tan(x)*arctan(x)';
var res = string.replace(/\b(?:arc)?tan\b/g,'np.$&');
console.log(res);
Explanation:
/ : regex delimiter
\b : word boundary, make sure we don't have any word character before
(?:arc)? : non capture group, literally 'arc', optional
tan : literally 'tan'
\b : word boundary, make sure we don't have any word character after
/g : regex delimiter, global flag
Replace:
$& : means the whole match, ie. tan or arctan
You can use regular expression to solve your issue. Following code is in javascript. Since, u didn't mention the language you are using.
var string = 'tan(x)*arctan(x)*xxxtan(x)';
console.log(string.replace(/([a-z]+)?(tan)/g,'np.$1$2'));

remove backslash from string lua

I working with some url string and i tried to remove "\" from the string to use url for my further use.
But when i tried using strin.gsub its not working as it should. rather then its giving me wrong output.
the String is
nas="\\192.168.1.220\STORAGE_1d1b7\a\b\c"
Code I have tried:
nas=string.gsub(nas,'\\',"")
print(nas)
Output:
192.168.1.220STORAGE_1d1b7??c
Output i need:
192.168.1.220STORAGE_1d1b7_a_b_c
its removing the "\" but it also affecting the "\" with "?"
i don't know where the "?" comes from?
The character \ is used to escape some special characters in a string, for eg.: \n represents a newline character (ASCII code 10) etc. (\a is ASCII code 7 in C/C++)
So, you'd need to define your string as:
nas = "\\\\192.168.1.220\\STORAGE_1d1b7\\a\\b\\c"
Alternatively, lua provides another way to define raw strings:
nas = [[\\192.168.1.220\STORAGE_1d1b7\a\b\c]]
Any ways Figured it out....
NASLocation = NASLocation:gsub('\\\\', ''):gsub('\\', '_',1):gsub('\\','/')

Resources