0

I'm trying to split a binary string of data like this:

trama = b'1 ; 12.0073 ; NAN ; NAN\r\n919.537 ; 1082.14 ; 0\r\n0 ; 850.26 ; NAN\r\n0 ; 0 ; 0\r\n0 ; 0 ; #\r\n1 ; 11.9612 ; NAN ; NAN\r\n933.792 ; 1097.16 ; 0\r\n0 ; 846.597 ; NAN\r\n0 ; 0 ; 0\r\n0 ; 0 ; #\r\n'

What I want is find the # separator because it means that at this point a set of data ends and consequently the following are another different sample.

I want this:

[
  ['1', '12.0073', 'NAN', 'NAN', '919.537', '1082.14', '0', '0', '850.26', 'NAN', '0', '0', '0', '0', '0'],
  ['1', '11.9612', 'NAN', 'NAN', '933.792', '1097.16', '0', '0', '846.597', 'NAN', '0', '0', '0', '0', '0']
]

Right now I'm doing all this process:

trama = b'1 ; 12.0073 ; NAN ; NAN\r\n919.537 ; 1082.14 ; 0\r\n0 ; 850.26 ; NAN\r\n0 ; 0 ; 0\r\n0 ; 0 ; #\r\n1 ; 11.9612 ; NAN ; NAN\r\n933.792 ; 1097.16 ; 0\r\n0 ; 846.597 ; NAN\r\n0 ; 0 ; 0\r\n0 ; 0 ; #\r\n'

values = [
    i.strip().decode() for i in trama.split()
    if i.strip().decode() not in [";"]
]


a, b = [], []
for i in values:
  if i != '#':
    b.append(i)
  else:
    a.append(b)
    b = []

It works, but I'm sure exists an easier way to do the same. Somebody knows a pythonic way to achieve it?

2 Answers 2

1

Split on the # first.

trama = b'1 ; 12.0073 ; NAN ; NAN\r\n919.537 ; 1082.14 ; 0\r\n0 ; 850.26 ; NAN\r\n0 ; 0 ; 0\r\n0 ; 0 ; #\r\n1 ; 11.9612 ; NAN ; NAN\r\n933.792 ; 1097.16 ; 0\r\n0 ; 846.597 ; NAN\r\n0 ; 0 ; 0\r\n0 ; 0 ; #\r\n'

lines = trama.decode('UTF-8').split('#')

records = []

for line in lines:
    if not line.strip(): continue
    records.append([v.strip() for v in line.replace('\r\n', ';').split(';') if v.strip()])

for values in records:
    print(values)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but I can't use re
Edited to remove re.
0

@Lleims Your question is not detailed enough but I will help you anyway.

I see 4 different separators:

  • '#' : record separator
  • '\r\n' : line separator (useless)
  • ';' : item separator
  • ' ' : spacer (useless)

I will assume that trama ends with '#\r\n' or '#' or '\r\n' but nothing else.
I also assume there are no space, '#' or '\r\n' inside item strings.

Here is the code:

    trama = b'...'    
    s = trama.decode().replace( '\r\n', '').replace( ';', '').rstrip( '#')    
    records = []    
    recs = s.split( '#') # get a list of records    
    for rec in recs: records.append( rec.split()) # get a list of lists

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.