Jump to content

Python split help

Go to solution Solved by fizzlesticks,

After splitting add a check to see if the last element is valid, if it isn't delete it.

if not split_string[-1]:   split_string.pop()

edit: or add a call to strip before splitting.

Hey guys, I'm trying to split a string at \n but there is a \n at the end of the string. It is creating an empty list and I'm getting an error when I want to loop through it. Can anyone give me a fix to this problem?

def read_coords(s):    split_string = ''     alive_cells = []         split_string = s.split('\n')        for x in range(len(split_string)):        for y in range(len(split_string)):            if split_string[x][y] == 'O':                alive_cells.append('(%d,%d)' %(x, y))        read_coords("O..\n.OO\n")
Link to comment
https://linustechtips.com/topic/473100-python-split-help/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×