Split a String with Unknown Number of Spaces as Separator in Python

Split a string with unknown number of spaces as separator in Python

If you don't pass any arguments to str.split(), it will treat runs of whitespace as a single separator:

>>> ' 1234    Q-24 2010-11-29         563   abc  a6G47er15'.split()
['1234', 'Q-24', '2010-11-29', '563', 'abc', 'a6G47er15']

split string by arbitrary number of white spaces

Just use my_str.split() without ' '.


More, you can also indicate how many splits to perform by specifying the second parameter:

>>> ' 1 2 3 4  '.split(None, 2)
['1', '2', '3 4 ']
>>> ' 1 2 3 4 '.split(None, 1)
['1', '2 3 4 ']

python split a string with at least 2 whitespaces

>>> import re    
>>> text = '10DEUTSCH GGS Neue Heide 25-27 Wahn-Heide -1 -1'
>>> re.split(r'\s{2,}', text)
['10DEUTSCH', 'GGS Neue Heide 25-27', 'Wahn-Heide', '-1', '-1']

Where

  • \s matches any whitespace character, like \t\n\r\f\v and more
  • {2,} is a repetition, meaning "2 or more"

Python - Split string of numeric values with unknown delimiters

I would like to split on any number of consecutive spaces, tabs, and commas.

You could use re.split() to split by a regular expression.

>>> import re
>>> s = '0 0 .1 .05 .05 0. 0. .01'
>>> re.split(r'[\s,]+', s)

['0', '0', '.1', '.05', '.05', '0.', '0.', '.01']

Note: The above will split accordingly on whitespace and commas. If you want to split strictly on <space>, tabs and commas, you could change the regular expression to [ \t,]+ ...

Split string on whitespace in Python

The str.split() method without an argument splits on whitespace:

>>> "many   fancy word \nhello    \thi".split()
['many', 'fancy', 'word', 'hello', 'hi']

python split string on whitespace

I see that you have several \t sometimes. I'd use the re module to split correctly:

for line in lines:
linedata = re.split(r'\t+', line)
print ",".join(linedata)

Python split string by space and strip newline char

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

Python: Split a line of numbers separated by spaces

perhaps convert number_line to a string with str(number_line)

How do I read in numbers (separated with whitespace) from a file?

You need to go through each line of the file, extract all numbers out of the line by splitting on whitespace, and then append those numbers to a list.

numbers = []

#Open the file
with open('file.txt') as fp:
#Iterate through each line
for line in fp:

numbers.extend( #Append the list of numbers to the result array
[int(item) #Convert each number to an integer
for item in line.split() #Split each line of whitespace
])

print(numbers)

So the output will look like

[27, 4, 390, 43, 68, 817, 83]


Related Topics



Leave a reply



Submit