7

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?

1
  • use parameter substitution instead of manually constructing sql string e.g., cursor.execute('select * from commands where cmd=?', (cmd,)). You don't need to call cursor.fetchall() you could iterate over it directly: for row in cursor: ... Commented Jan 16, 2012 at 0:18

3 Answers 3

16

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This exists?! The python standard libraries never stop surprising me. +1
I was working on a dirty trick to get the job done. I had no idea something like this was available. I'll definitely take a look once I get the chance. Thanks!
It's quick and it gets the job done. That's all I was looking for :)
2

Split on spaces to separate words, then join 8 at a time with a space as the separator.

content = "This is some sentence that has more than eight words"
content = content.split(" ")
print content
['This', 'is', 'some', 'sentence', 'that', 'has', 'more', 'than', 'eight', 'words']
print(" ".join(content[0:8]))
This is some sentence that has more than

1 Comment

Thanks! This was what I was looking for, for now.
1

Cut by words instead of characters using python textwrap module:

>>> import textwrap
>>> text = 'asdd sdfdf asdsg asfgfhj'
>>> s = textwrap.wrap(text, width=10)  # <- example 10 characters
>>> s
['asdd sdfdf', 'asdsg', 'asfgfhj']
>>> print '\n'.join(s)
asdd sdfdf
asdsg
asfgfhj
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.