How to put special character between string [duplicate] - python

This question already has answers here:
How to convert hexadecimal string to bytes in Python?
(7 answers)
Closed 9 years ago.
I have a string like "0013A200305EFF96". I want to change it to be in form "\x00\x13\xA2\x00\x30\x5E\xFF\x96". The special character is "\x". How can I do this in a time efficient way?

Python2
>>> "0013A200305EFF96".decode("hex")
'\x00\x13\xa2\x000^\xff\x96'
Python3
>>> bytes.fromhex("0013A200305EFF96")
b'\x00\x13\xa2\x000^\xff\x96'

gnibbler's answer is probably what you are really looking for; but for completeness, here is how you can insert any sequence:
>>> '\\x'.join(a[i:i+2] for i in xrange(0, len(a), 2))

If you mean literal \x:
import re
s= "0013A200305EFF96"
s=re.sub("(..)", r"\x\1",s)
print s
Output
\x00\x13\xA2\x00\x30\x5E\xFF\x96

Related

How to replace several overlapped characters with one in Python [duplicate]

This question already has answers here:
How do I coalesce a sequence of identical characters into just one?
(10 answers)
Closed 2 years ago.
I have a string, something like that (I don't know in advance how much similar characters in a sequence):
s = '&&&&&word&&&word2&&&'
and would like to obtain as a result this string:
'&word&word2&'
Workaround is something like this (not effective I guess for large texts):
while True:
if not '&&' in s:
break
s = s.replace('&&','&')
You can use a regex to replace any occurence of one or more '&' (&+) by '&':
import re
s = '&&&&&word&&&word2&&&'
res = re.sub(r'&+', '&', s)
print(res)
# &word&word2&

How to find 6 digits in a string with a particular pattern on the first 3 digits in Python? [duplicate]

This question already has answers here:
re.findall behaves weird
(3 answers)
Closed 2 years ago.
I am trying to find a regular expression to return to me the entire 6 digits with the first 3 digits as a pattern/fixed.
Ex:
import re
string_ex = 'docs/data/622999/2013904065003_file.bin'
re.findall(r'622(\d{3})',string_ex)
results in just ['999']
but I want the result to be ['622999']
Thanks!
You should include 622 too within the parenthesis
>>> import re
>>> string_ex = 'docs/data/622999/2013904065003_file.bin'
>>> re.findall(r'(622\d{3})',string_ex)
['622999']
You can use "index" on the string directly.
i = string_ex.index("622")
found = string_ex[i-3:i+2]
https://www.tutorialspoint.com/python/string_index.htm

Capture repeated characters and split using Python [duplicate]

This question already has answers here:
How can I tell if a string repeats itself in Python?
(13 answers)
Closed 3 years ago.
I need to split a string by using repeated characters.
For example:
My string is "howhowhow"
I need output as 'how,how,how'.
I cant use 'how' directly in my reg exp. because my input varies. I should check the string whether it is repeating the character and need to split that characters.
import re
string = "howhowhow"
print(','.join(re.findall(re.search(r"(.+?)\1", string).group(1), string)))
OUTPUT
howhowhow -> how,how,how
howhowhowhow -> how,how,how,how
testhowhowhow -> how,how,how # not clearly defined by OP
The pattern is non-greedy so that howhowhowhow doesn't map to howhow,howhow which is also legitimate. Remove the ? if you prefer the longest match.
lengthofRepeatedChar = 3
str1 = 'howhowhow'
HowmanyTimesRepeated = int(len(str1)/lengthofRepeatedChar)
((str1[:lengthofRepeatedChar]+',')*HowmanyTimesRepeated)[:-1]
'how,how,how'
Works When u know the length of repeated characters

Eliminate numbers in string in Python [duplicate]

This question already has answers here:
Remove specific characters from a string in Python
(26 answers)
Removing numbers from string [closed]
(8 answers)
Closed 8 years ago.
I’d like to eliminate numbers in a string in Python.
str = "aaaa22222111111kkkkk"
I want this to be "aaaakkkkk".
I use re.sub to replace, but it doesn't work:
str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)
Maybe, this replaces a string which only contains numbers with "".
How should I do with this?
your regex is wrong:
re.sub(r'[0-9]',"",str)
should work:
>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'

Python decode "\u041b" string [duplicate]

This question already has an answer here:
How can I convert strings like "\u5c0f\u738b\u5b50\u003a\u6c49\u6cd5\u82f1\u5bf9\u7167" to Chinese characters
(1 answer)
Closed 9 years ago.
I have unicode string, i'm sure that it's UTF-8, but I can't decode it. The string is '\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'. How to decode it?
You can use aString.decode('unicode_escape'), it convert a unicode-format string to unicode object
>>> u'\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'
u'\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'
>>> '\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'.decode('unicode_escape')
u'\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'
>>>
In your case
>>> print '\u041b\u0435\u0433\u043a\u043e\u0432\u044b\u0435'.decode('unicode_escape')
Легковые
>>>

Categories

Resources