user7218495 user7218495 - 5 days ago 5
Python Question

How could I split this string in python

I'm learning Python (3). I would like to split each of these lines into four separate pieces.

[Test Branch] bobjones 0 6/13/2008 4:24 PM
[Todd's Workspace] tfatcher 0 6/16/2008 9:20 AM
[Henry] hmckinkley 1 6/17/2008 10:12 AM
[Henry] hmckinkley 0 6/17/2008 10:15 AM


I could call line.split() on each one, but then I'd put the date back together. And I guess the spaces in the first [ ] section rules that out also. I suppose I could slice it, but I'm not 100% sure this data is as fixed width as it seems. A regex is probably best, eh? Any pointers on that?

Update:
I thought @Selcuk's solution this was going to work great:

branch,user,version,timestamp = [commitheaderline.split("]", 1)[0] + "]"] + commitheaderline.split("]", 1)[1].split(None, 2)


But then I encountered some data where the username was too long (example below), so the rest of the data ended up on a new line. So I'm working on that now. I'm thinking I test the line somehow before running the split(), and if it doesn't look like a "proper" line I'll join it with the next one.

[Test Branch] bobjones 0 6/13/2008 4:24 PM
[Todd's Workspace] tfatcher 0 6/16/2008 9:20 AM
[cole] bob.darknsdale
0 7/27/2012 12:49 PM

Answer

You could do the following:

[line.split("]", 1)[0] + "]"] + line.split("]", 1)[1].split(None, 2)

which will result in

['[Test Branch]', 'bobjones', '0', '6/13/2008 4:24 PM']
Comments