简体   繁体   中英

Python split string by space and strip newline char

I have a string in python 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're 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're two spaces in the middle of the matrix and there're newlines chars at the end of each line. So I got:

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

I just want the numbers to be in the list, not anything else, anybody knows how to do it? Thanks for your time in advance.

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']

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']

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM