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'
Jump to Post
You 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', …
All 5 Replies
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.