LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-17-2011, 07:31 PM   #1
recuse44
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Rep: Reputation: 0
[PYTHON] Split a string by every nth characters


Hi, I need some help splitting a string by every nth characters.
I'm using Python 2.7.1 because I'm using older libraries, if that matters.

For example, if this is my input:
Quote:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac elit nibh, vitae venenatis ligula. Vestibulum a varius turpis.
Splitting it by every 10th character should produce this list:
Quote:
[ "Lorem ipsu", "m dolor si", "t amet, co", "nsectetur ", "adipiscing", " elit. Sed", " ac elit n", "ibh, vitae", " venenatis", " ligula. V", "estibulum ", "a varius t", "urpis." ]
Any help would be appreciated
 
Old 01-17-2011, 08:28 PM   #2
cin_
Member
 
Registered: Dec 2010
Posts: 266

Rep: Reputation: 23
slice notation

What are you doing with the results?

Look into slices...http://docs.python.org/tutorial/introduction.html

Last edited by cin_; 01-17-2011 at 08:39 PM. Reason: gramm'err
 
Old 01-19-2011, 02:41 AM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
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.
 
Old 01-19-2011, 05:52 PM   #4
recuse44
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the answers, works great
I'm doing this to format my debug logs so that they are more readable.
 
Old 01-20-2011, 04:34 AM   #5
cin_
Member
 
Registered: Dec 2010
Posts: 266

Rep: Reputation: 23
cut()

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 04:47 AM. Reason: gramm'err
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 06:57 PM
string split ovince Programming 4 06-10-2007 06:45 PM
split string prob izza_azhar Programming 3 02-08-2005 01:11 AM
split string izza_azhar Programming 6 01-18-2005 09:24 PM
Need to split an input string general4172 Linux - Software 6 10-30-2003 12:57 AM


All times are GMT -5. The time now is 03:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration