Hi,
I'm a newbie to python and I'm reading a file and trying to split a string every 3 spaces, that is included any whitespace. string.split() will split the string up every whitespace, but is there another way to specify every 3 spaces, whether there is whitespace or not? I'm trying to do this example:
string = '491 43140113108107 11210'
and split it every 3 spaces. So I can pull out the numbers:
string = '491 43 140 113 108 107 11 210'
Morika
0
Newbie Poster
Recommended Answers
Jump to PostYou can do it like this.
>>> s = '491 43140113108107 11210' >>> s '491 43140113108107 11210' >>> def splitCount(s, count): return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])] >>> a = splitCount(s, 3) >>> a ['491', ' 43', '140', '113', '108', '107', ' 11', …
Jump to PostTake copy or type correct.
You have type wrong here.b = ''.join(a) ---> b = ' '.join(a)
Rember space.' '
All 5 Replies
snippsat
661
Master Poster
Morika
0
Newbie Poster
snippsat
661
Master Poster
sravan953
Morika
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.