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
#1
Hello,
I want to make the following :
today I'm reading canbus data in this form (the data is string type)
Data = d8 59 80 a2 c8 be f4 92
I want to make a function the take the data, put every 2 digits into 1 place in a list
so in the end I will have this :
Data_List = [d8, 59, 80, a2, c8, be, f4, 92]
then I will create a function that will print the data for place 0-7 according to input
Data_List[3] --> 'a2'

the reason in to make an easy list and not to think how to get to item 3 (places 9 and 10)
so insted of writing
Data[9:11] I will wirte Data_List[3]
hope the reason is clear

I mange to to this:
def CutData(FullData, StartByte, length=1):
    Byte_Data  = []
    print(f'full data is {FullData}')
    print(f'size is {len(FullData)}')
    i = 0
    test = ""
    for Char in FullData:
        print(str(i) + " : " + Char )
        if Char  != " ":
            test = test + Char 
            Byte_Data.insert(i, test)
            i += 1
        else:
            test = ""
    print(Byte_Data)
    Byte_Data = Byte_Data[1::2]
    print(Byte_Data)
which retunr this:

full data is d8 59 80 a2 c8 be f4 92
size is 23
0 : d
1 : 8
2 :
2 : 5
3 : 9
4 :
4 : 8
5 : 0
6 :
6 : a
7 : 2
8 :
8 : c
9 : 8
10 :
10 : b
11 : e
12 :
12 : f
13 : 4
14 :
14 : 9
15 : 2
['d', 'd8', '5', '59', '8', '80', 'a', 'a2', 'c', 'c8', 'b', 'be', 'f', 'f4', '9', '92']
['d8', '59', '80', 'a2', 'c8', 'be', 'f4', '92']
I guess this is OK - but is there more "normal" \ pretty way to do this?

Thanks ,
Reply


Messages In This Thread
enter string into list according to a known rule - by korenron - Aug-11-2021, 01:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,699 Sep-21-2023, 09:17 AM
Last Post: ineuw
  Pythone module for dynamic rule execution hemal07yc 1 9,025 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,276 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  Create Alert if string from list appears on other list javalava 1 2,534 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,542 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython
  Create a new list by comparing values in a list and string DBS 2 3,563 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