I have a list of bytes and I want to convert it to a list of strings, in python I use this decode function:
x=[b'\xd8\xa8\xd9\x85\xd8\xb3\xd8\xa3\xd9\x84\xd8\xa9',
b'\xd8\xa5\xd9\x86\xd8\xb4\xd8\xa7\xd8\xa1',
b'\xd9\x82\xd8\xb6\xd8\xa7\xd8\xa1',
b'\xd8\xac\xd9\x86\xd8\xa7\xd8\xa6\xd9\x8a',
b'\xd8\xaf\xd9\x88\xd9\x84\xd9\x8a']
y=[ a.decode("utf-8") for a in x]
How can I get the same result in Tensorflow?
thank you
You can use tf.compat.as_str_any().
for i in x:
print(tf.compat.as_str_any(i))
will print list items as python strings. No need to use session.
Related
I'm trying to get this string into list, how can i do that pleas ?
My string :
x = "[(['xyz1'], 'COM95'), (['xyz2'], 'COM96'), (['xyz3'], 'COM97'), (['xyz4'], 'COM98'), (['xyz5'], 'COM99'), (['xyz6'], 'COM100')]"
I want to convert it to a list, so that:
print(list[0])
Output : (['xyz1'], 'COM95')
If you have this string instead of a list, that presumes it is coming from somewhere outside your control (otherwise you'd just make a proper list). If the string is coming from a source outside your program eval() is dangerous. It will gladly run any code passed to it. In this case you can use ast.liter_eval() which is safer (but make sure you understand the warning on the docs):
import ast
x = "[(['xyz1'], 'COM95'), (['xyz2'], 'COM96'), (['xyz3'], 'COM97'), (['xyz4'], 'COM98'), (['xyz5'], 'COM99'), (['xyz6'], 'COM100')]"
l = ast.literal_eval(x)
Which gives an l of:
[(['xyz1'], 'COM95'),
(['xyz2'], 'COM96'),
(['xyz3'], 'COM97'),
(['xyz4'], 'COM98'),
(['xyz5'], 'COM99'),
(['xyz6'], 'COM100')]
If the structure is uniformly a list of tuples with a one-element list of strings and an individual string, you can manually parse it using the single quote as a separator. This will give you one string value every other component of the split (which you can access using a striding subscript). You can then build the actual tuple from pairing of two values:
tuples = [([a],s) for a,s in zip(*[iter(x.split("'")[1::2])]*2)]
print(tuples[0])
(['xyz1'], 'COM95')
Note that this does not cover the case where an individual string contains a single quote that needed escaping
You mean convert list like string into list? Maybe you can use eval().
For example
a="[1,2,3,4]"
a=eval(a)
Then a become a list
to convert as list use x = eval(x)
print(list[0]) will give you an error because list is a python builtin function
you should do print(x[0]) to get what you want
I am working on an IPV4 breakdown where I have the necessary values in a string variable to represent the binary
(example: 00000000.00000000.00001111.11111111) This is a string
I need a way to turn this string into binary to then properly convert it to it's proper integer value
(in this case 0.0.15.255)
I've seen posts asking about something similar but attempting to apply it to what I'm working on has been unsuccessful
Apologies if this made no sense this is my first time posing a question here
You can achieve this using int() with base argument.
You can know more about int(x,base) - here
Split the string at '.' and store it in a list lst
For every item in lst, convert the item (binary string) to decimal using int(item, base=2) and then convert it into string type.
Join the contents of lst using .
s = '00000000.00000000.00001111.11111111'
lst = s.split('.')
lst = [str(int(i,2)) for i in lst]
print('.'.join(lst))
# Output
0.0.15.255
First split the string on . then convert each to integer equivalent of the binary representation using int builtin passing base=2, then convert to string, finally join them all on .
>>> text = '00000000.00000000.00001111.11111111'
>>> '.'.join(str(int(i, base=2)) for i in text.split('.'))
# output
'0.0.15.255'
You should split the data, convert and combine.
data = "00000000.00000000.00001111.11111111"
data_int = ".".join([str(int(i, 2)) for i in data.split(".")])
print(data_int) # 0.0.15.255
Welcome! Once you have a string like this
s = '00000000.00000000.00101111.11111111'
you may get your integers in one single line:
int_list = list(map(lambda n_str: int(n_str, 2), s.split('.')))
What is the best way to convert the values in tuples from unicode to string, when the tuples are in a list, can it be done without looping?
unicodedata.normalize('NKFD', x) can only take unicode, not a tuple. The dataset also includes float values.
EXAMPLE
unicode_tuple_list = [(u'text in unicode', u'more unicode'), (u'more text in unicode', u'even more unicode')]
print type(unicode_tuple_list) # list - keep as list
print type(unicode_tuple_list[0]) # tuple - keep as tuple
print type(unicode_tuple_list[0][0]) # unicode
How can all these values be made a str?
I'm not sure there is a way to convert this without using a loop/list comprehension.
I would use the map function to accomplish this, see:
unicode_tuple_list = [(u'text in unicode', u'more unicode'), (u'more text in unicode', u'even more unicode')]
string_tuple_list = [tuple(map(str,eachTuple)) for eachTuple in unicode_tuple_list]
print string_tuple_list
Unpack the tuples, convert to a string and repack.
tuple(map(str, unicode_tuple_list))
I am trying to cast a python list into a float.
This is the problem narrowed down:
loss = ['[228.55112815111235]', '[249.41649450361379]']
print(float(loss[0]))
And results in the error:
ValueError: could not convert string to float: '[231.49377550490459]'
Can anyone help me?
Strip the brackets.
float(loss[0].replace('[', '').replace(']', ''))
You can use string slicing if there is always just one element in your string list.
loss = ['[228.55112815111235]', '[249.41649450361379]']
print(float(loss[0][1:-1]))
That is because your float value is encapsulated within brackets. And you'll get ValueError because that is not valid float value. In order to convert it, you have to firstly remove them. For example:
>>> my_val = '[228.55112815111235]'
>>> print float(my_val[1:-1])
228.551128151
In your case, you have to write:
>>> loss = ['[228.55112815111235]', '[249.41649450361379]']
>>> float(loss[0][1:-1])
228.55112815111235
In case you want to convert entire list to list of float, you may use map() function as:
>>> map(lambda x: float(x[1:-1]), loss)
[228.55112815111235, 249.4164945036138]
If you want to convert the list values to floats you can use list comprehension:
loss = [float(loss[i][1:-1]) for i in range(len(loss))]
Then your loss list will look like this:
[228.55112815111235, 249.4164945036138]
What is the easier way to convert 110_181 to 11.0.181?
I was able to convert 110_181 to 11.0.181 using following str.replace function.
.replace("_",".")
How can I convert 110.181 to 11.0.181?
If string size and format will be same always, you can use slice operation and str.join:
>>> s = '110_181'
>>> '.'.join((s[:2], s[2], s[4:]))
'11.0.181'