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 9 months ago.
Improve this question
I have a text file containing bits so it’s like „1000101011010110000…“ in this text. I want python to interpret this text as bytes and perform different byte transformations with it. But how do I red it on as bytes without python thinking it’s a string?
The built-in int function has an parameter base for specifying the base.
To convert a string into an integer with base 2 (binary), pass 2 into it:
s = input()
num = int(s, 2)
# Manipulate `num` as you like
Related
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 1 year ago.
Improve this question
I want to strip 0 from a given string.
The string contains either 1 or 0. I want to strip the zeroes if they appear at the ends.
I know i can do this using if condition, but i want to know if there is any function made to do this efficiently than using if-else.
Example-
String = 0100010101010
Output = 10001010101
Also, i don't think using regex is any more efficient, complexity wise.
Try this:
s = "0100010101010"
print(s.lstrip("0").rstrip("0"))
'10001010101'
This should work for the string s:
s = s.strip("0")
Make sure s is a string and not a number.
Can you try this , it will work
s = str(s).strip("0")
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 6 years ago.
Improve this question
I need to convert the format of this number:
CB0D8A83 7FBC1D22 86388A2D AFA0B9A1
I read this number:
ciphertext= (ser.read(45))
print(ciphertext)
Ciphertxt_file.write(ciphertext)
to this format:
cb0d8a837fbc1d2286388a2dafa0b9a1
I would be very grateful if yo could help me.
Simply convert them to lowercase with .lower() and remove the whitespace with .replace(' ',''):
result = ciphertext.lower().replace(' ','')
No need to use advanced tools here. This is however not a binary format (the binary format uses zeros and ones like 00110101101110). Furthermore this method does not check the format so 1134ZZ223 would be accepted as well.
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
Sample Input (in Plaintext)
1
1 2
Description- The first line contains an integer t, denoting the number of test cases. Next t lines contain two integers, a and b separated by a space.
My question is how can I load these inputs into variables using Python 2.7? Also I need to load this into IDE without using "xxx.py > input.txt"
raw_input() takes in a line of input as a string. The .split() string method produces a list of the whitespace-separated words in a string. int() parses a string as an int. So putting that together, you can read the input you want using
t = int(raw_input())
cases = []
for i in range(t):
cases.append( map(int, raw_input().split()) )
This will give you a list of a,b pairs if the input is in the correct format, but won't do any error checking.
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.
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 Have string, from some xml element:
source = "<![CDATA[<p><span stylefontfamily times new romantimesserif fontsize large>Is important ?</span></p>]]
"
I wanna Remove specified string base on this list:
mustDelWord=["<p>","</p>","<span stylefontfamily times new romantimesserif fontsize large>","</span>"]
So, the expected output is:
<![CDATA[Is important ?]]>
So far code is :
mustDelWord=["<p>","</p>","<span stylefontfamily times new romantimesserif fontsize large>","</span>"]
for i in mustDelWord:
source = source.replace(mustDelWord[i],"")
print source
But occur this error:
source = source.replace(mustDelWord[i],"")
TypeError: list indices must be integers, not str
Thanks.
Just change the line to:
source = source.replace(i,"")
This is because the loop for i in mustDelWord already iterates over the elements in the list, not the indices in that list, so you don't have to do mustDelWord[i].