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 6 2951
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
-- http://mail.python.org/mailman/listinfo/python-list
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
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
"Jeffrey Maitland" <ma***@vianet.ca> wrote in message
news:ma**************************************@pyth on.org... 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
-- http://mail.python.org/mailman/listinfo/python-list 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
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.
On Friday 10 December 2004 10:29 am, Jon wrote: 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/
Jon wrote: 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
--
[Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer
jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen
jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs-
mitteln inkompatibel. -- Florian Diesch in dcoulm
Jon wrote: 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: import regex
__main__:1: DeprecationWarning: the regex module is deprecated; please
use the re module dir(regex)
['__doc__', '__name__', 'casefold', 'compile', 'error', 'get_syntax',
'match', 'search', 'set_syntax', 'symcomp'] import re dir(re)
['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:
s = "i wanted to be a lumberjack!" s.title()
'I Wanted To Be A Lumberjack!'
Steve
Jon wrote: 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? s = """\
.... Well, he's...
.... he's, ah...
.... probably pining for the fjords.""" import string print string.capwords(s)
Well, He's... He's, Ah... Probably Pining For The Fjords. print s.title()
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Damien |
last post by:
I have just built a simple stopwatch application, but when i f5 to get
things goings i get this message,
An unhandled exception of type 'System.ArithmeticException' occurred in...
|
by: Brandon J. Van Every |
last post by:
I'm realizing I didn't frame my question well.
What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump
up in your chair and scream "Wow! Ruby has *that*? That is SO...
|
by: |
last post by:
Hi folks!
Can't compile my file due to some problems with crypt module.
My platform is WinXP:
First I launch my Python Shell,
Than I open the *.py file,
Next I press F5 to 'run module'
The...
|
by: marco |
last post by:
I'm trying to parse a xml bookmarkpage with php. I found a very useful
example script about how you can parse a xml document with php. The
scriptworks really smooth. The xml test document (See...
|
by: Ask |
last post by:
G'day All,
Just thought I'd drop in and say hi. I'm new to Python, but old to software
development.
Python is one of the languages used in my new job, so I've just bought a
book, read it, and...
|
by: Bernardo |
last post by:
Hi,
In Visual Basic there are modules where I can store public variables, Constants, etc.
I have in a C# program a class that have a method to open a form.
public void GUIGestaoAcessos(string...
|
by: Fred Nelson |
last post by:
Hi:
I have programmed in VB.NET for about a year and I'm in the process of
learing C#. I'm really stuck on this question - and I know it's a "newby"
question:
In VB.NET I have several...
|
by: Fred Nelson |
last post by:
Hi:
I'm working on a VS2005 web application and I have what is probabably a
"newby" question.
In VS2003 I could drag a textbox/button/etc on to a form and position it
with the mouse. I...
|
by: SimonPalmer |
last post by:
Apologies in advance if this is either a) the wrong board or b) been
answered a million times elsewhere, but...
I have been given an assignment to get a python module up and running
behind an...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |