Trying to use replace function (python) - python-3.8

I am learning python ,i.e., version 3.8
While trying to replace a string substring using replace() function.
str = "Python"
str.replace("th", "t")
print(str)
Output:
Python
Expected Output:
Pyton

replace() returns a copy of the string with all occurrences of
substring old replaced by new
You need to assign to a variable to store the copy
str = "Python"
replaced_str= str.replace("th", "t")
print(replaced_str)

Related

Replace function not working as expected with Dask

I'm reading a dask dataframe:
ddf = dd.read_csv({...}, dtype='object')
Next, I'm trying to replace commas with dots, so values can injected in a SQL DB as floats.
ddf = ddf.replace(",", ".")
However, when I'm call ddf.to_sql({...}) my code is returning ValueError: Unable to parse string "2,0" at position 8, which suggests that the replace function is not working as expected. Why is this the case? Is there another way to replace commas with dots in Dask?
You need to use regex here (right now you're replacing a single-character string ","):
ddf = ddf.replace("[,]", ".", regex=True)

How to get only 3 string after # from #get.com or #getting.com in VBA

How to get only 3 string after # from #get.com or #getting.com in VBA
for example i have to achieve #get from provided string #get.com or #getting.com
You can use the Instr() function to locate the # in your string.
Then use Mid() to substring n chars starting from this location

Netezza SQL - How to replace string between two Comma's, with a condition

I have a string where I want to find :N between two commas, and replace the data data between two commas with blank. Example shown below.
Currently its being done manually with exporting it into excel, making changes and importing it back into database. I'm sure there would be a better way to have this done in coding without the need of export/import.
Example:
"This is sting one:Y,this is string Two:X,This is string Three:N,This is string four:N,This is string five:X,"
Desired outcome:
"This is sting one:Y,this is string Two:X,This is string five:X,"
Would really appreciate your help.
A
I would use the sql extensions toolkit and regexp_replace.
I've found that the nz implementation of regex seems to have some issues with the ? non-greedy modifier on my version. The expression that worked here was ,[^(:N)]+:N.
FORTUNE_DB(ADMIN)=> select * from so;
COL1
--------------------------------------------------------------------------------------------------------------
This is sting one:Y,this is string Two:X,This is string Three:N,This is string four:N,This is string five:X,
FORTUNE_DB(ADMIN)=> select regexp_replace(col1,',[^(:N)]+:N','') from so;
REGEXP_REPLACE
-----------------------------------------------------------------
This is sting one:Y,this is string Two:X,This is string five:X,

How to Split String into Array for multiple characters

Here is my old script, in vb
Dim strArray As String() = str.Split(New Char() {":"C})
This works fine if the char is only :
But now I want to split for this,
:.++.:
As my str is complicated is there a way to split it ?
Edit :
My
str = hello:.++.:sAwesome Right ? yeah://.,]['; :.++.:
nvijuds789g34huve02qjgv0b0whgvn0iegvb0wvi0hn
so after split
strArray(0) = hello
strArray(1)= sAwesome Right ? yeah://.,][';
strArray(2) = nvijuds789g34huve02qjgv0b0whgvn0iegvb0wvi0hn
I think you see what I need. I am the one adding :.++.: before each part in previous functions. It is because the text almost contains every single character and I can't control it :/
thats why i used some complicated combination of characters to make it impossible for the file to contain it
You can split a string by another string using the String.Split overload (String(), StringSplitOptions).
Example:
Debug.WriteLine(String.Join("|", "A:.++.:B:.++.:C:.++.:D".Split({":.++.:"}, StringSplitOptions.RemoveEmptyEntries)))
Outputs:
A|B|C|D

Why HIVE must split string with "\01"? [b4 0.11.0 is yes, after 0.11.0 you can specified]

There is some string in a hive table, I use transform method that replace some char, my mapper script like this:
<?php
$strFrom = "\7";
$strTo = "\1"; // "|" it works well
$fd = fopen("php://stdin", "r");
while($line = fgets($fd)){
$outStr = str_replace($strFrom, $strTo, $line);
print $outStr;
}
fclose($fd);
my hive sql like this:
select transform (value)
using 'home/php/bin/php -c home/php/etc/php.ini replace.php'
as (v1 string)
from test_tbl
actually I try to replace string from "\7" to "\1", but I find it seems replace correctly, but it just output the first column. One input like this:
a\7b\7c\7d
then it output like this:
a
yeah, just one column!
If I replace it to "|", it output:
a|b|c|d
So I am confused, why must hive split string with "\1"? How can I forbid it? I just want to get:
a\1b\1c\1d
I found my answer in here.
Data written to the filesystem is serialized as text with columns separated by ^A and rows separated by newlines.
As of Hive 0.11.0 the separator used can be specified, in earlier versions it was always the ^A character (\001)
Thanks for all guys that seen this question.