Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43380 > unrolled thread

My string module doesn't have maketrans or translate functions

Started byLamb <lambandme@gmail.com>
First post2013-04-11 08:30 -0700
Last post2013-04-12 00:17 +0000
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  My string module doesn't have maketrans or translate functions Lamb <lambandme@gmail.com> - 2013-04-11 08:30 -0700
    Re: My string module doesn't have maketrans or translate functions Chris Angelico <rosuav@gmail.com> - 2013-04-12 01:45 +1000
      Re: My string module doesn't have maketrans or translate functions Lamb <lambandme@gmail.com> - 2013-04-11 09:05 -0700
        Re: My string module doesn't have maketrans or translate functions Chris Angelico <rosuav@gmail.com> - 2013-04-12 02:09 +1000
      Re: My string module doesn't have maketrans or translate functions Lamb <lambandme@gmail.com> - 2013-04-11 09:05 -0700
    Re: My string module doesn't have maketrans or translate functions Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-04-11 17:56 +0100
      Re: My string module doesn't have maketrans or translate functions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-12 00:17 +0000

#43380 — My string module doesn't have maketrans or translate functions

FromLamb <lambandme@gmail.com>
Date2013-04-11 08:30 -0700
SubjectMy string module doesn't have maketrans or translate functions
Message-ID<338cca79-08c6-4c6f-89a4-d3a50ece3dee@googlegroups.com>
Hi all,
 I'm really new to python and trying to figure out the basic rule and settings of it. I'm using python 3.3 and I was trying this code in python:

import string
s = "string. With. Punctuation?" 
out = s.translate(string.maketrans("",""), string.punctuation)

And I got the following error:
Traceback (most recent call last):
  File "C:/Python33/trials/ls.py", line 5, in <module>
    out = s.translate(string.maketrans("",""), string.punctuation)
AttributeError: 'module' object has no attribute 'maketrans'

So I used the following to see functions inside string
e=dir(string)
>>> print(e)
['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']

It doesn't have translate(), maketrans(), rstrip() and a whole lot of other things. What's the problem?

Thanks!

[toc] | [next] | [standalone]


#43384

FromChris Angelico <rosuav@gmail.com>
Date2013-04-12 01:45 +1000
Message-ID<mailman.484.1365695113.3114.python-list@python.org>
In reply to#43380
On Fri, Apr 12, 2013 at 1:30 AM, Lamb <lambandme@gmail.com> wrote:
> import string
> s = "string. With. Punctuation?"
> out = s.translate(string.maketrans("",""), string.punctuation)

Try this instead:

import string
s = "string. With. Punctuation?"
out = s.translate(str.maketrans("", "", string.punctuation))

Due to the changes in string handling (s is a Unicode string in Python
3, not a string of bytes), str.translate() got changed. Check out
help(str.maketrans) and help(str.translate) in interactive Python for
more details.

ChrisA

[toc] | [prev] | [next] | [standalone]


#43385

FromLamb <lambandme@gmail.com>
Date2013-04-11 09:05 -0700
Message-ID<5dbca83a-f976-4582-8022-8f8c67f759e6@googlegroups.com>
In reply to#43384
Thanks! It worked! But why didn't I see functions : translate(), maketrans(), rstrip(), etc. listed when I called print(dir(string))?

On Thursday, April 11, 2013 11:45:05 AM UTC-4, Chris Angelico wrote:
> 
> > import string
> 
> > s = "string. With. Punctuation?"
> 
> > out = s.translate(string.maketrans("",""), string.punctuation)
> 
> 
> 
> Try this instead:
> 
> 
> 
> import string
> 
> s = "string. With. Punctuation?"
> 
> out = s.translate(str.maketrans("", "", string.punctuation))
> 
> 
> 
> Due to the changes in string handling (s is a Unicode string in Python
> 
> 3, not a string of bytes), str.translate() got changed. Check out
> 
> help(str.maketrans) and help(str.translate) in interactive Python for
> 
> more details.
> 
> 
> 
> ChrisA

[toc] | [prev] | [next] | [standalone]


#43387

FromChris Angelico <rosuav@gmail.com>
Date2013-04-12 02:09 +1000
Message-ID<mailman.486.1365696582.3114.python-list@python.org>
In reply to#43385
On Fri, Apr 12, 2013 at 2:05 AM, Lamb <lambandme@gmail.com> wrote:
> Thanks! It worked! But why didn't I see functions : translate(), maketrans(), rstrip(), etc. listed when I called print(dir(string))?

Because they're not in the string module any more - they're methods on
str (and bytes). Try checking out dir(str) - str being the class of
your basic Unicode string now, rather than being the type of a string
of bytes - and you should see them all.

ChrisA

[toc] | [prev] | [next] | [standalone]


#43386

FromLamb <lambandme@gmail.com>
Date2013-04-11 09:05 -0700
Message-ID<mailman.485.1365696329.3114.python-list@python.org>
In reply to#43384
Thanks! It worked! But why didn't I see functions : translate(), maketrans(), rstrip(), etc. listed when I called print(dir(string))?

On Thursday, April 11, 2013 11:45:05 AM UTC-4, Chris Angelico wrote:
> 
> > import string
> 
> > s = "string. With. Punctuation?"
> 
> > out = s.translate(string.maketrans("",""), string.punctuation)
> 
> 
> 
> Try this instead:
> 
> 
> 
> import string
> 
> s = "string. With. Punctuation?"
> 
> out = s.translate(str.maketrans("", "", string.punctuation))
> 
> 
> 
> Due to the changes in string handling (s is a Unicode string in Python
> 
> 3, not a string of bytes), str.translate() got changed. Check out
> 
> help(str.maketrans) and help(str.translate) in interactive Python for
> 
> more details.
> 
> 
> 
> ChrisA

[toc] | [prev] | [next] | [standalone]


#43391

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-04-11 17:56 +0100
Message-ID<mailman.488.1365699359.3114.python-list@python.org>
In reply to#43380
On 11/04/2013 16:30, Lamb wrote:
> Hi all,
>   I'm really new to python and trying to figure out the basic rule and settings of it. I'm using python 3.3 and I was trying this code in python:
>
> import string
> s = "string. With. Punctuation?"
> out = s.translate(string.maketrans("",""), string.punctuation)
>
> And I got the following error:
> Traceback (most recent call last):
>    File "C:/Python33/trials/ls.py", line 5, in <module>
>      out = s.translate(string.maketrans("",""), string.punctuation)
> AttributeError: 'module' object has no attribute 'maketrans'
>
> So I used the following to see functions inside string
> e=dir(string)
>>>> print(e)
> ['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']
>
> It doesn't have translate(), maketrans(), rstrip() and a whole lot of other things. What's the problem?
>
> Thanks!
>

The string module is effectively dead.  Many functions were deprecated 
back in 2.6, maketrans in 3.1, just use string methods instead.

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#43406

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-04-12 00:17 +0000
Message-ID<51675286$0$29977$c3e8da3$5496439d@news.astraweb.com>
In reply to#43391
On Thu, 11 Apr 2013 17:56:51 +0100, Mark Lawrence wrote:

> The string module is effectively dead.

It's not dead, it's pining for the fjords.

But seriously, the string module holds a collection of useful string 
constants and the Template class. 


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web