添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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..

with open(file) as f: content = f.readlines() sub ='\x02' new = ("\n".join(s for s in content if sub.lower() in s.lower())) for an example C00K13M4N Jun 10, 2018 at 11:36
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]
                I Got a little Problem there cause my readlines() retrieves a list, and i cant split lists.
– C00K13M4N
                Jun 10, 2018 at 11:41
                Your list only had one element so I assumed you were good to get it. You could try placing all my code in a loop if there are more than one item in the list. I don't see why there would be. But I have updated to reflect your comment
– John
                Jun 10, 2018 at 11:45
                Solved it by list[0].split. Works great for the first Measurement but there are others following... Like `len(list[0]) = 60000' and 'len(items) = 600' so there are like 9 more measurements. can you help me writing a loop for this?
– C00K13M4N
                Jun 10, 2018 at 11:54

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']
                Is it more readable? I see you striping the same delimiter you then split on. Sure it works, but explicit is better than implicit.
– John
                Jun 10, 2018 at 11:48
                @John yes because it strips from the ends and splits whatever is left, for me it's simpler
– Ofer Sadan
                Jun 10, 2018 at 11:49

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.