Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I got a serious Problem which gives me a headache.
I have a Log File of a Sensor with different measurement values, They all appear between a substring '\x02' and '\x03'. But here starts my question. How am I able to get all values between the substrings into a new list. As there are many of measurements and not only one it should provide as many lists as there are '\x02' and '\x03'.
To get you a quick overview how the Logfile is looking after i read it into python:
['\x02sEA LMDscandata 1\x03\x02sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1\x03']
I realy tried a lot (searching substrings, searching for indices and so on..) and cant help myself out, I´m looking forward to your suggestions.
THANK YOU
EDIT1:
I need seperated Lists between the \x02 and \x03 statements. For an example use:
['\x02sEA LMDscandata 1\x03\x02sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1\x03 \x02sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1\x03']
So the first List should look like that'list1= [sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1'] and the second one like the same. I need a for loop for this, but i cant imagine how to implement it..
–
listfromreadlines = ['\x02sEA LMDscandata 1\x03\x02sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1\x03']
l1 = listfromreadlines[0]
items = [x.replace('\x02', "").replace('\x03', "") for x in l1.split('\x03\x02')]
# Thinking you might have sub items between the delimiters, I can only assume
# they are split by spaces:
subitems = [x.split(" ") for x in items]
–
–
–
Pretty much the same as John's answer, but simpler and more readable:
l1 = '\x02sEA LMDscandata 1\x03\x02sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1\x03'
items = l1.strip('\x03\x02').split('\x03\x02')
print(items)
# prints: ['sEA LMDscandata 1', 'sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1']
–
–
I suggest using a regular expression:
>>> lines = ['\x02sEA LMDscandata 1\x03\x02sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1\x03']
>>> import re
>>> [s for line in lines for s in re.findall('\x02(.*?)\x03', line)]
['sEA LMDscandata 1', 'sSN LMDscandata 0 1 10B2E77 0 0 5BB6 E4FC 5FA60C99 5FA8C92F F0 0 0 0 F5B2 3E8 1']
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.