[Newby] question about modules

J

Jon

Hi,

The following four lines of code:

import sys, os, re
sentence = raw_input("Enter a sentence: ")
capwords (sentence)
print sentence

gives me the following error: NameError: name 'capwords' is not defined

As far as I can tell from the online docs, "capwords" should be defined in
the built-in "regex" module. Why is it telling me that capwords is not
defined?

I am completely new to Python so my apologies for such a basic question!

Thanks,
Jon
 
J

Jeffrey Maitland

Jon writes:
Hi,

The following four lines of code:

import sys, os, re
sentence = raw_input("Enter a sentence: ")
capwords (sentence)
print sentence

gives me the following error: NameError: name 'capwords' is not defined

As far as I can tell from the online docs, "capwords" should be defined in
the built-in "regex" module. Why is it telling me that capwords is not
defined?

I am completely new to Python so my apologies for such a basic question!

Thanks,
Jon

Hello Jon,

The reason for that is you only imported the module(class/object) now to use
its methodes you need to call the object.

in this case to fix the problem you are having you would have to do it
either one of these methodes (unless there are more that I am not aware of)
1/
import sys, os, re
sentence = raw_input("Enter a sentence: ")
re.capwords (sentence) # <------ notice the re.capwords this
# calling the capword method of the re module.
print sentence

2/
import sys, os
from re import * # <---- import all methods of re and allow them to be
# used locally
sentence = raw_input("Enter a sentence: ")
capwords (sentence)
print sentence
# this imports all the methodes in the re module so they can be used
# localy.

or
import sys, os
from re import capwords # <---- import only capwords method of re
sentence = raw_input("Enter a sentence: ")
capwords (sentence)
print sentence
# this import only imports the capwords methode of the re module


I hope this helps you some and if i used the incorrect terminology I am
sorry and I hope someone points it out to me.

Jeff Maitland
 
J

Jon

Hi Jeff,

That makes sense -- thanks. However now when I use "re.capwords (sentence)"
I get a different error message:

AttributeError: 'module' object has no attribute 'capwords'

Each of the other two suggested implimentations produce a similar error
message. Is there something even more basic that I am failing to do? I'm
using the IDLE GUI in WinXP, Python release 2.4...

Thanks!
Jon
 
J

James Stroud

Is it in "regex" or "re"? If in "re" then:

re.capwords(sentence)

If in "regex", then:

regex.capwords(sentence)


You can also do

from re import *

then you will not have to prefix. But careful not to clutter your namespace.

Hi,

The following four lines of code:

import sys, os, re
sentence = raw_input("Enter a sentence: ")
capwords (sentence)
print sentence

gives me the following error: NameError: name 'capwords' is not defined

As far as I can tell from the online docs, "capwords" should be defined in
the built-in "regex" module. Why is it telling me that capwords is not
defined?


--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
611 Charles E. Young Dr. S.
MBI 205, UCLA 951570
Los Angeles CA 90095-1570
http://www.jamesstroud.com/
 
R

Reinhold Birkenfeld

Jon said:
Hi Jeff,

That makes sense -- thanks. However now when I use "re.capwords (sentence)"
I get a different error message:

AttributeError: 'module' object has no attribute 'capwords'

Each of the other two suggested implimentations produce a similar error
message. Is there something even more basic that I am failing to do? I'm
using the IDLE GUI in WinXP, Python release 2.4...

The 'capwords' function seems to be defined in the "string" module, not
the "re" module.

Reinhold
 
S

Steven Bethard

Jon said:
The following four lines of code:

import sys, os, re
sentence = raw_input("Enter a sentence: ")
capwords (sentence)
print sentence

gives me the following error: NameError: name 'capwords' is not defined

As far as I can tell from the online docs, "capwords" should be defined in
the built-in "regex" module. Why is it telling me that capwords is not
defined?

Which docs are you looking at? I couldn't find documentation for a
capwords method or class... Checking at the interactive prompt:
__main__:1: DeprecationWarning: the regex module is deprecated; please
use the re module['__doc__', '__name__', 'casefold', 'compile', 'error', 'get_syntax',
'match', 'search', 'set_syntax', 'symcomp']['DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'U',
'UNICODE', 'VERBOSE', 'X', '__all__', '__builtins__', '__doc__',
'__file__', '__name__', 'compile', 'engine', 'error', 'escape',
'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sub',
'subn', 'template']

I don't see an object named capwords in either module.

What are you trying to do? If you just want to capitalize words,
str.title() is probably the easier way to do this:
'I Wanted To Be A Lumberjack!'

Steve
 
S

Steven Bethard

Jon said:
As far as I can tell from the online docs, "capwords" should be defined in
the built-in "regex" module. Why is it telling me that capwords is not
defined?

Hmm... are you looking instead for "capwords" from the string module?
.... Well, he's...
.... he's, ah...
.... probably pining for the fjords."""Well, He'S...
He'S, Ah...
Probably Pining For The Fjords.

Note that there are a few subtle differences between string.capwords and
str.title -- string.capwords capitalizes only at whitespace boundaries
(and replaces runs of whitespace with spaces), while str.title
capitalizes at alphanumeric boundaries.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top