Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a variable length string into multiple parts in python

I have a database:

As you can see in the 'desc' column, the text is of variable length (meaning no two strings I pull from this database are going to be of the same length). I will eventually add many more entries to this database, but this is what I'm testing with and starting with at the moment.

Right now, I have the following python code to grab these blocks of string and display them:

cmd = input(Enter command:)
sql = "SELECT cmd,`desc` FROM table WHERE cmd = '"+ cmd +"'"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
    print("Command: "+ row[0] +":\n")
    print("Description: "+ row[1][:40] +"\n")
    if (len(row[1]) > 40):
       print(row[1][40:85])
    if (len(row[1]) > 85):
       print(row[1][85:130])
    if (len(row[1]) > 130):
       print(row[1][130:165])
    if (len(row[1]) > 165):
       print(row[1][165:])

The splitting here works to an extent, for example:

Command: close:
Description: This command will create a 'close' butto
n in the message window for the invoking char
acter. If no window is currently on screen, t
he script execution will end.

As you can see with the example above of the output, the split causes some characters to get cut off in mid word. Given the fact that the strings could be of any length between say...20 total characters and up to 190ish, and I want to split the string into chunks of say...8 words each because of space constraints, how would I go about doing this?

like image 692
Jguy Avatar asked Jan 15 '12 23:01

Jguy


People also ask

How do you split a string into multiple elements in Python?

To split a String in Python with a delimiter, use split() function. split() function splits the string into substrings and returns them as an array.

How do you split a variable in Python?

To split string variables at each whitespace, we can use the split() function with no arguments. The syntax for split() function is split(separator, maxsplit) where the separator specifies the character at which the string should be split. maxsplit specifies the number of times the string has to be split.

How do you split a sentence into multiple lines in Python?

Python String splitlines() method is used to split the lines at line boundaries. The function returns a list of lines in the string, including the line break(optional).


1 Answers

Check out the textwrap module.

>>> import textwrap
>>> 
>>> s = "This command will create a 'close' button in the message window for the invoking character. If no window is currently on screen, the script execution will end."
>>> 
>>> wrapped = textwrap.wrap(s, 40)
>>> 
>>> for line in wrapped:
...     print line
... 
This command will create a 'close'
button in the message window for the
invoking character. If no window is
currently on screen, the script
execution will end.

You can do a lot of configuration of TextWrapper.

like image 183
DSM Avatar answered Nov 11 '22 23:11

DSM