[SOLVED] [PYTHON] Split a string by every nth characters
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
Quote:
Hi, I need some help splitting a string by every nth characters.
As has been said, you should first look into string slicing and also list comprehensions will help to build your required list. This little bit of code does both and creates a function I've named split with takes the parameters of the string and a length to split into:
Code:
def split(input, size):
return [input[start:start+size] for start in range(0, len(input), size)]
if __name__=="__main__":
mystring="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac elit nibh, vitae venenatis ligula. \
Vestibulum a varius turpis."
print split(mystring, 10)
As you can see the function itself is just one line of python code the rest is a little test stub to see that the function works as expected - obviously by no means an exact unit test but it shows it works.
Quote:
I'm using Python 2.7.1 because I'm using older libraries, if that matters.
Well 2.7.1 is the latest release in the 2.x series - I would still advise anyone starting to stick with this series rather than the 3.x release - if only as there are still quite a lot of libraries not ported to 3.x yet.
Sounds like a good project to start manipulating strings with python.
You can also check out cut(), it might have some functionality that would interest you...
Code:
# man cut
#
...great for piping...
Code:
# tail -n17 /var/log/messages | grep MARK | cut -c1-3,7-15,25,27,29-33
Jan 16:21:20 -MARK
Jan 17:01:20 -MARK
Jan 17:21:20 -MARK
Jan 17:41:20 -MARK
Jan 18:01:20 -MARK
Jan 18:21:20 -MARK
Jan 18:41:20 -MARK
Jan 19:01:20 -MARK
# tail -n4 /var/log/messages | grep MARK
Jan 19 16:21:20 cin_ -- MARK --
#
Last edited by cin_; 01-20-2011 at 03:47 AM.
Reason: gramm'err
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.