Python Forum
enter string into list according to a known rule
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
enter string into list according to a known rule
#7
But if I want to converte only 1 item in list?
for example item on 4 place only?

def CutData(FullData, StartByte, length=1):
    StartByte = StartByte - 1
    EndByte = StartByte + length
    print(f'full data is {FullData}')
    Data_Byte = FullData.split(' ')
    print(f'Full Data_Byte is {Data_Byte}')
    Wanted_Data = str(Data_Byte[StartByte:EndByte])
    print(type(Wanted_Data))
    return Wanted_Data

if PID == "1E491DD0":
                Speed= (CutData(Data, 4, 1))
                print (str(type(Speed)) + "  " + Speed)
I get
full data is e5 a9 5c 7e b2 ff 02 00
Full Data_Byte is ['e5', 'a9', '5c', '7e', 'b2', 'ff', '02', '00']
<class 'str'>
<class 'str'>  ['b2']
when I do the same thing on console , it's working

>>> Test_List = ['12', '34', 'AA', 'F2']
>>> type(Test_List)
<class 'list'>
>>> type(Test_List[2])
<class 'str'>
>>> y = int(Test_List[2], 16)
>>> print(y)
170
also if I take your example - it's working (same class - both of them are strings)

>>> spam = bytearray(b'0P\x14}\x04\xf8\xff3')
>>> data = [f"{byte:02x}" for byte in spam]
>>> print(data)
['30', '50', '14', '7d', '04', 'f8', 'ff', '33']
>>> type(data[2])
<class 'str'>
>>> y = int(data[2], 16)
>>> print(y)
20
Thanks ,
Reply


Messages In This Thread
RE: enter string into list according to a known rule - by korenron - Aug-12-2021, 07:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,704 Sep-21-2023, 09:17 AM
Last Post: ineuw
  Pythone module for dynamic rule execution hemal07yc 1 9,026 Sep-13-2019, 11:25 AM
Last Post: luoheng
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,281 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  Create Alert if string from list appears on other list javalava 1 2,535 Sep-17-2018, 02:44 PM
Last Post: DeaD_EyE
  List of pathlib.Paths Not Ordered As Same List of Same String Filenames QbLearningPython 20 15,554 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython
  Create a new list by comparing values in a list and string DBS 2 3,565 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020