I have string say
string1 = '0x000x200x300x00'
I want result like
result = '\x00\x20...'
I want to replace every '0x' with '\x' . How to do it ?
I have tried replace function in python like as follows
y = x.replace('0x', '\x')
y = x.replace('0x', '\\x')
y = x.replace('0x', r'\x')
But no success :( .
Can anyone help in this issue ?
string1.replace("0x","\\x") use `\\` to escape the `\`
/ is also not the same as\.
string1 = '0x000x200x300x00'
print string1.replace("0x","\\x")
\x00\x20\x30\x00
In [19]: string1 = '0x000x200x300x00'
In [20]: print string1.replace("0x","\\x") # str
\x00\x20\x30\x00
In [21]: string1.replace("0x","\\x") # repr
Out[21]: '\\x00\\x20\\x30\\x00'
What exactly are your trying to accomplish?
I am a bit wary to post this answer because it does not directly answer your question, but I assume you want to do some byte-mangling (wild guess) as you were not very explicit in your question.
If that's the case, then here's another "thing" you might want to do:
data = [int(_, 16) for _ in string1.split('0x')[1:]]
This will give you a list of ints representing the "byte"(?) values (of course depends what you want to do...)
The above code makes a couple of assumptions based on your example input:
The input string will start with 0x
The input string does not include any non-escaped values. In other words, you always have a sequence of 0xnn strings with nothing else in between!
Related
I am trying to use a variable inside a substructure. I guess the variable should be of integer data type, and I am trying to add a loop here but it my data type is list since it contains multiple integers.
INV_match_id = [['3749052'],['3749522']]
from statsbombpy import sb
for x in range(2):
match=INV_match_id[x]
match_db = sb.events(match_id=match)
print(match)
I have tried to extract the data one by one using another variable, but still it got declared as list. Whenever I give direct values to "match" it works. for eg: if I add a line match=12546 the substructure takes the value properly.
Next thing I want to try is hard declare "match" variable as integer. Any input is appreciated. I am pretty new to Python.
Edit: Adding this solution from #quamrana here.
"So, to answer your original question: Is it possible to hard declare a variable in Python?, the answer is No. Variables in python are just references to objects. Objects can be of whatever type they want to be."
You said: " I want to loop and take the numbers one by one."
Did you mean this:
for match in INV_match_id:
match_db = sb.events(match_id=match)
I don't know what you want to do with match_db
Update:
"that single number is also declared as a list. like this- ['125364']"
Well if match == ['125364'] then it depends on whether you want: "125364" or 125364. I assume the latter since you talk a lot about integers:
for match in INV_match_id:
match = int(match[0])
match_db = sb.events(match_id=match)
Next Update:
So you have: INV_match_id = ['3749052','3749522']
This means that the list is a list of strings, so the code changes to this:
for match in INV_match_id:
match_db = sb.events(match_id=int(match))
Your original code was making match into a list of the digits of each number. (eg match = [1,2,5,3,6,4])
Reversionary Update:
This time we have: INV_match_id = [['3749052'],['3749522']]
that just means going back to the second version of my code above:
for match in INV_match_id:
match = int(match[0])
match_db = sb.events(match_id=match)
It's as simple as:
from statsbombpy import sb
INV_match_id = [['3749052'],['3749522']]
for e in INV_match_id:
match_db = sb.events(match_id=e[0])
print(match_db)
You have a list of lists albeit that the sub-lists only contain one item.
match_id can be either a string or int
I got this code which does something with the first 10 charakters of a string:
f_binary = f.encode(encoding='utf_8')[0:10]
but I want to do it with the 19th charakter as well. I tried like this:
f_binary = f.encode(encoding='utf_8')[0:10],[19]
and this:
f_binary = f.encode(encoding='utf_8')[0:10,19] but it doesn't work.
Python's list comprehension doesn't help me either because it doesn't show how to deal with a larger and a small part of a list or string at the same time.
Just use
f_binary = (f[0:10]+f[-1]).encode(encoding='utf_8')
to encode the first 10 and the last character of string f
turn it to a string and then select using
first_n_chars_and_last = (f[0:n], f[-1])
and turn it THEN into a bytes object
Python beginner here, sorry if this is a dumb question.
So I have a long string, and I need to add a character in very specific areas of the strings. For example, a | after character number 23, 912, and 1200. I read this Add string in a certain position in Python, but it only works for adding one character.
Also, the solution needs to be expandable, not just do it 3 times. The code I'm making can have lots of different locations with where I want the character to be.
With reference to the link that you posted Add string in a certain position in Python;
If you would like to repeat the operation for different values, you could create a list containing all index positions where you would like your | character to be inserted.
For example,
>>> l = [1, 3, 4]
>>> s = "abcdef"
>>> for i in l:
>>> s = s[:i] + "|" + s[i:] # as suggested in your link
>>> s
'a|b||cdef'
This will allow you to repeat the process for the set of values that you provide in the list. You could also define a function to assist in this, which I could explain if this method is insufficient!
Note, however, that this will insert the character relative to the current iteration. That is, in this example, after adding the | at position 1, the next insert position, 3, is different from what it was before the first insert. You could avoid this (if you want) by including a counter variable to offset all the index positions by the number of inserts that have been executed (will require initial list to be ordered).
Not so good at python, hope I can help
According to that site you went to, you can make a while loop to solve the problem
The code should look something like this
def insert_dash(string, index, addin):
return string[:index] + addin + string[index:]
alldone = False
string = input("String: ")
index = " "
while index:
index = input("Index: ")
addin = input("Add into: ")
string = insert_dash(string, index, addin)
Hope it helps!
PS: I have NOT tried the code, but I think it will work
I have a question about python string.
I'm trying to make a wx.listbox, and I want to input 2 items.
In this case can't I give a space between 2 string and integer?
for item in test():
self.Append('%s %d'%(str(item.name), item.cnt))
I want to give a gap between %s and %d using variable.
The space is there, maybe just not quite visible, you may need something like str.ljust to make the appended strings aligned. E.g.:
In [14]: for i, s in enumerate(['name', 'looongname']):
...: print '%s%d'%(s.ljust(20), i)
#prints out:
name 0
looongname 1
When you want to join two strings just use the join-function. There you can choose what to use between the strings:
' '.join([(str(item.name), item.cnt])
You can put everything between the quotes.
I am working with python and I am new to it. I am looking for a way to take a string and split it into two smaller strings. An example of the string is below
wholeString = '102..109'
And what I am trying to get is:
a = '102'
b = '109'
The information will always be separated by two periods like shown above, but the number of characters before and after can range anywhere from 1 - 10 characters in length. I am writing a loop that counts characters before and after the periods and then makes a slice based on those counts, but I was wondering if there was a more elegant way that someone knew about.
Thanks!
Try this:
a, b = wholeString.split('..')
It'll put each value into the corresponding variables.
Look at the string.split method.
split_up = [s.strip() for s in wholeString.split("..")]
This code will also strip off leading and trailing whitespace so you are just left with the values you are looking for. split_up will be a list of these values.