Split string by space and strip newline char

Question:

I have a string that looks like:

d4 c3 b2 a1 02 00 04 00  00 00 00 00 00 00 00 00 
ff ff 00 00 01 00 00 00  00 00 00 00 00 00 00 00 
36 00 00 00 36 00 00 00  00 1c 23 10 f8 f1 00 1b 
17 01 10 20 08 00 45 00  00 28 df 27 40 00 80 06 
2b 87 c7 08 1a 0a 0a 05  05 0a 5c ea 5c ea c2 1f 

There are many more lines that I skipped. I want to put each of the numbers in a list. When I use .split, it returns me a list of not just numbers, but also spaces and n‘s, because there are two spaces in the middle of the matrix and there are newlines at the end of each line. So I got:

['d4', 'c3', 'b2', 'a1', '02', '00', '04', '00', '', '00', …, 'nff', 'ff', '00'…]

How can I get just the numbers to be in the list, not anything else?

Asked By: J Freebird

||

Answers:

If you use .split(" "), then your program will split on every single space, and not on any other whitespace. If you use .split() instead, the program will take into account multiple spaces, newlines, tabs and all other forms of whitespace. That should get you what you’re looking for.

>>> teststr = "a   v w   ef sdv   n   wef"
>>> print(teststr)
a   v w   ef sdv   
   wef
>>> teststr.split()
['a', 'v', 'w', 'ef', 'sdv', 'wef']
>>> teststr.split(" ")
['a', '', '', 'v', 'w', '', '', 'ef', 'sdv', '', '', 'n', '', '', 'wef']
Answered By: TheSoundDefense

The python string documentation has a huge list of things you can do to strings.

What’s odd is that the .split() is not eliminating all of your whitespace when, as mentioned above by TheSoundDefense, it should.

To get rid of the newlines, try using the .replace(<target>,<replacement>) method like so:

a = '11 11 11 11  11 11 11 11 n22 22 22 22  22 22 22 22 n'
b = a.replace('n',' ')
c = b.split()
print c
>>> ['11', '11', '11', '11', '11', '11', '11', '11', '22', '22', '22', '22', '22', '22', '22', '22']
Answered By: mlinsenbard

Using split() without any arguments will split the contents on any whitespaces and also group together several whitespaces.

Here is an example:

s = """d4 c3 b2 a1 02 00 04 00  00 00 00 00 00 00 00 00 
ff ff 00 00 01 00 00 00  00 00 00 00 00 00 00 00 
36 00 00 00 36 00 00 00  00 1c 23 10 f8 f1 00 1b 
17 01 10 20 08 00 45 00  00 28 df 27 40 00 80 06 
2b 87 c7 08 1a 0a 0a 05  05 0a 5c ea 5c ea c2 1f"""

data = s.split()

In this case, data will look like this:

['d4', 'c3', 'b2', 'a1', '02', '00', '04', '00', '00', '00', '00', '00', '00', '00', '00', '00', 'ff', 'ff', '00', '00', '01', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '00', '36', '00', '00', '00', '36', '00', '00', '00', '00', '1c', '23', '10', 'f8', 'f1', '00', '1b', '17', '01', '10', '20', '08', '00', '45', '00', '00', '28', 'df', '27', '40', '00', '80', '06', '2b', '87', 'c7', '08', '1a', '0a', '0a', '05', '05', '0a', '5c', 'ea', '5c', 'ea', 'c2', '1f']
Answered By: HAL
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.