Highlight some bytes in a string [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a list like following:
\xeb\x1f\x5e\x31\xdb\x88\x5e\x07\x89\x76\x08\x89\x5e\x0c\x8d\x1e\x8d\x4e\x08\x8d\x56\x0c\x31\xc0\xb8\x0b\x00\x00\x00\xcd\x80\x31\xf6\xe8\xdc\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x41\x42\x42\x42\x42\x43\x43\x43\x43
I want to find \x00 and \xcd\x80 in the string and print it in highlight style. (for example with red color.). How can I do that?

If the string s is unescaped (the \ are real, characters), you can for instance use:
print(s.replace(r'\x00','\x1b[31m\\x00\x1b[0m') \
.replace(r'\xcd\x80','\x1b[31m\\xcd\\x80\x1b[0m'))
What we do here is look for the raw string r'\x00' and replace it by '\x1b[31m\\\x00\x1b[0m'. This means we prepend it with '\x1b[31m', the ANSI terminal escape code for red foreground and append it with '\x1b[0m', the ANSI terminal escape code for dropping markup.
If I run this code with your string on my console, I get:
Now this is of course not very convenient. So you can use:
def print_highlight(s,markers=(r'\x00',r'\xcd\x80')):
for marker in markers:
s = s.replace(marker,'\x1b[31m%s\x1b[0m'%marker)
print(s)
So now you can give it the string s, together with a list of string fragments you want to highlight. For example:
print_highlight(string,(r'\x31',))
will highlight all \x31 parts.

Related

replace all int's in string with _ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
How can I separate data types in a string or list so they can be set to another character, I assume there is something I have missed but everything i have tried so far has now worked for me. so far have tried so split into list and use a for loop to find every int but I can't find a way to differentiate the data types so it can change every int.
You can use regex :
import re
re.sub("\d", "_", "10 4 2")
\d matches any decimal digit character.

Remove words in string if it contains \u in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to remove words in string if it contains \u in python?
ex:
string ="\uf064thickness cfoutside\uf0d7\uf03a\uf03d TC2019 45TRCMat"
The final output should be like this.
"TC2019 45TRCMat"
After removing all of the words if it contains \u.
Rather then looking to remove unicode character go the other way and only allow ascii character:
string ="\uf064thickness cfoutside\uf0d7\uf03a\uf03d TC2019 45TRCMat"
def is_ascii(s):
return all(ord(c) < 128 for c in s)
for s in string.split(" "):
if is_ascii(s):
print(s)
Reference: How to check if a string in Python is in ASCII?

How to remove all characters from a string after two white spaces in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.

Python opposite of strip() [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to figure out a function that does the opposite of strip for my Python class.
The exact question is:
What function can be used to surround a string with spaces in order to make it a certain length?
Thanks!
The "opposite" of strip is the center method:
>>> 'Hello, World!'.center(30)
' Hello, World! '
You can specify the fill character to use as second argument. The default is whitespace.
There are also ljust and rjust that will left-justify and right-justify the text up to a certain length, and as such could be considered "opposites" of lstrip and rstrip.
format strings
"%50s World"%("Hello")
"%-50s World"%("Hello")
"%^50s World"%("Hello")
or newfangled
"{0:50s} World".format("Hello")
"{0:>50s} World".format("Hello")
"{0:^50s} World".format("Hello")
these have nifty side effects
"%^*s World"%(50,"Hello")
Try s.center(N) where N is the line length you want, s - your string.
For example:
>>> "xx".center(10)
' xx '

What's the best way to convert an integer (0-255) to an escaped byte (\x00 - \xff)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Basically what I'm asking is, what's the most direct way to convert any integer between 0 and 255 into it's hexadecimal, escaped equivalent? One that I mean will function correctly if wrapped in a write() function (which means '\x56' writes 'V' and not literally '\x56'.
That's what the chr function is for.
f.write(chr(0x56))
Speaking of hexadecimal escaped equivalents isn't really relevant in this context - every character has a hexadecimal equivalent, but in expressing a string the characters that can be expressed as a single simple character are simply output as the character.

Categories

Resources