Replace multiple symbols using replace() - python

How can I replace multiple symbols with the method replace()? Is it possible to do that with just one replace()? Or is there any better ways?
The symbols can look like this for example -,+,/,',.,&.

You can use re.sub and put the characters in a character class:
import re
re.sub('[-+/\'.&]', replace_with, input)

You may do it using str.join with generator expression (without importing any library) as:
>>> symbols = '/-+*'
>>> replacewith = '.'
>>> my_text = '3 / 2 - 4 + 6 * 9' # input string
# replace char in string if symbol v
>>> ''.join(replacewith if c in symbols else c for c in my_text)
'3 . 2 . 4 . 6 . 9' # Output string with symbols replaced

# '7' -> 'A', '8' -> 'B'
print('asdf7gh8jk'.replace('7', 'A').replace('8', 'B'))

You can only do one symbol replace, what you could do is create the old strings and new strings list and loop them:
string = 'abc'
old = ['a', 'b', 'c']
new = ['A', 'B', 'C']
for o, n in zip(old, new):
string = string.replace(o, n)
print string
>>> 'ABC'

Related

python how to split string with more than one character?

I would like to split a string as below
1234ABC into 123 and ABC
2B into 2 and B
10E into 10 and E
I found split function does not work because there is no delimiter
You can use itertools.groupby with boolean isdigit function.
from itertools import groupby
test1 = '123ABC'
test2 = '2B'
test3 = '10E'
def custom_split(s):
return [''.join(gp) for _, gp in groupby(s, lambda char: char.isdigit())]
for t in [test1, test2, test3]:
print(custom_split(t))
# ['123', 'ABC']
# ['2', 'B']
# ['10', 'E']
This can quite easily be accomplished using the re module:
>>> import re
>>>
>>> re.findall('[a-zA-Z]+|[0-9]+', '1234ABC')
['1234', 'ABC']
>>> re.findall('[a-zA-Z]+|[0-9]+', '2B')
['2', 'B']
>>> re.findall('[a-zA-Z]+|[0-9]+', '10E')
['10', 'E']
>>> # addtionall test case
...
>>> re.findall('[a-zA-Z]+|[0-9]+', 'abcd1234efgh5678')
['abcd', '1234', 'efgh', '5678']
>>>
The regex use is very simple. Here is quick walk through:
[a-zA-Z]+: Match one or more alphabetic characters lower case or upper
| or...
[0-9]+: One or more whole numbers
Another way to solve it using re package
r = re.search('([0-9]*)([a-zA-Z]*)', test_string)
r.groups()

Find all strings in nested brackets

How do i find string in nested brackets
Lets say I have a string
uv(wh(x(yz))
and I want to find all string in brackets (so wh, x, yz)
import re
s="uuv(wh(x(yz))"
regex = r"(\(\w*?\))"
matches = re.findall(regex, s)
The above code only finds yz
Can I modify this regex to find all matches?
To get all properly parenthesized text:
import re
def get_all_in_parens(text):
in_parens = []
n = "has something to substitute"
while n:
text, n = re.subn(r'\(([^()]*)\)', # match flat expression in parens
lambda m: in_parens.append(m.group(1)) or '', text)
return in_parens
Example:
>>> get_all_in_parens("uuv(wh(x(yz))")
['yz', 'x']
Note: there is no 'wh' in the result due to the unbalanced paren.
If the parentheses are balanced; it returns all three nested substrings:
>>> get_all_in_parens("uuv(wh(x(yz)))")
['yz', 'x', 'wh']
>>> get_all_in_parens("a(b(c)de)")
['c', 'bde']
Would a string split work instead of a regex?
s='uv(wh(x(yz))'
match=[''.join(x for x in i if x.isalpha()) for i in s.split('(')]
>>>print(match)
['uv', 'wh', 'x', 'yz']
>>> match.pop(0)
You could pop off the first element because if it was contained in a parenthesis, the first position would be blank, which you wouldn't want and if it wasn't blank that means it wasn't in the parenthesis so again, you wouldn't want it.
Since that wasn't flexible enough something like this would work:
def match(string):
unrefined_match=re.findall('\((\w+)|(\w+)\)', string)
return [x for i in unrefined_match for x in i if x]
>>> match('uv(wh(x(yz))')
['wh', 'x', 'yz']
>>> match('a(b(c)de)')
['b', 'c', 'de']
Using regex a pattern such as this might potentially work:
\((\w{1,})
Result:
['wh', 'x', 'yz']
Your current pattern escapes the ( ) and doesn't treat them as a capture group.
Well if you know how to covert from PHP regex to Python , then you can use this
\(((?>[^()]+)|(?R))*\)

Get a list from config.ini file

In my config file I have something like that :
[Section_1]
List=Column1,Column2,Column3,Column4
Now, I would like to process it in my main file as normal lists :
config = configparser.ConfigParser()
config.read("configTab.ini")
for index in range(len(List)):
sql=sql.replace(List[index],"replace("+List[index]+","'hidden'")")
Now when I read from configuration file "List" is a normal String.
What is the best approach to to it?
If I put a normal list variable in my main code it this way:
List=['Column1','Column2','Column3','Column4']
Then it works fine, but I would like to get that from my configure file,
Thanks
Use str.split:
List = List.split(',')
string = 'a, b, c'
print(string.split(','))
>> ['a', 'b', 'c']
The answer by #DeepSpace is not entirely correct. The leading whitespace around 'b' and 'c' is included in the output if one executes this line as written (e.g. 'b' is actually ' b').
To avoid leading and trailing whitespace, try:
string = 'a, b, c'
print([i.strip() for i in string.split(',')])
>> ['a', 'b', 'c']
Use Regular expression to remove extra space and split with commas
re.py
pattern = re.compile("^\s+|\s*,\s*|\s+$")
str_list = 'a, b, c'
cis_list = pattern.split(str_list)
>> ['a', 'b', 'c']

Split string based on a regular expression

I have the output of a command in tabular form. I'm parsing this output from a result file and storing it in a string. Each element in one row is separated by one or more whitespace characters, thus I'm using regular expressions to match 1 or more spaces and split it. However, a space is being inserted between every element:
>>> str1="a b c d" # spaces are irregular
>>> str1
'a b c d'
>>> str2=re.split("( )+", str1)
>>> str2
['a', ' ', 'b', ' ', 'c', ' ', 'd'] # 1 space element between!!!
Is there a better way to do this?
After each split str2 is appended to a list.
By using (,), you are capturing the group, if you simply remove them you will not have this problem.
>>> str1 = "a b c d"
>>> re.split(" +", str1)
['a', 'b', 'c', 'd']
However there is no need for regex, str.split without any delimiter specified will split this by whitespace for you. This would be the best way in this case.
>>> str1.split()
['a', 'b', 'c', 'd']
If you really wanted regex you can use this ('\s' represents whitespace and it's clearer):
>>> re.split("\s+", str1)
['a', 'b', 'c', 'd']
or you can find all non-whitespace characters
>>> re.findall(r'\S+',str1)
['a', 'b', 'c', 'd']
The str.split method will automatically remove all white space between items:
>>> str1 = "a b c d"
>>> str1.split()
['a', 'b', 'c', 'd']
Docs are here: http://docs.python.org/library/stdtypes.html#str.split
When you use re.split and the split pattern contains capturing groups, the groups are retained in the output. If you don't want this, use a non-capturing group instead.
Its very simple actually. Try this:
str1="a b c d"
splitStr1 = str1.split()
print splitStr1

How do I convert string characters into a list? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create a list with the characters of a string?
Example:
'abc'
becomes
['a', 'b', 'c']
Is it a combination of split and slicing?
>>> x = 'abc'
>>> list(x)
['a', 'b', 'c']
Not sure what you are trying to do, but you can access individual characters from a string itself:
>>> x = 'abc'
>>> x[1]
'b'
If you need to iterate over the string you do not even need to convert it to a list:
>>> n = 'abc'
>>> for i in n:
... print i
...
a
b
c
or
>>> n[1]
'b'
yourstring = 'abc'
[char for char in yourstring]

Categories

Resources