5

I'm probably being really dumb here, but I can't figure out this error:

'str' object has no attribute 'punctuation'

This occurs on the line:

docLines[counter][counter2] = [(docLines[counter][counter2]).translate(None, string.punctuation)]

Where docLines[counter][counter2] is just a single word.

Any ideas where I'm going wrong with is line of code?

Community
  • 1
  • 1
djcmm476
  • 1,723
  • 6
  • 24
  • 46

1 Answers1

15

You've assigned a string (instance of str) to a variable named string. Rename the variable and the problem will go away.

To debug this, add print repr(string) before the offending line and it will print a string instance. A number of such prints in various places in your module will help you discover where the name string stopped referring to the string module and started referring to a str instance.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • 1
    This is perfect, but it's marginally worth noting that he also has to import the string module, if he has not already. – Alex V Nov 02 '12 at 15:16
  • I assumed the OP knows that, or if not, the `NameError` will guide him in the right direction. The error he did get, on the other hand, is quite confusing and not at all helpful if one doesn't already understand what's going on. – user4815162342 Nov 02 '12 at 15:21
  • Good point, I suppose it's pretty unlikely he'd be using string.punctuation if he _didn't_ already know it comes from the string module. – Alex V Nov 02 '12 at 15:24
  • I have been importing string, yes. I don't have any variables called string that I can see though. – djcmm476 Nov 02 '12 at 15:25
  • Add `print repr(string)` before the offending line and you'll see that it prints a string. A number of such prints in various places in your module will help you discover where the name `string` stopped referring to the `string` module and started referring to a `str` instance. – user4815162342 Nov 02 '12 at 15:28
  • @Incredidave I've now edited the answer to include the debugging tip from my last comment. – user4815162342 Nov 02 '12 at 15:30
  • I'll try that now, thanks. I suspect the problem will be that I'm reading in a file then using .readlines() and .split() to turn it into a list of lines, each of which is a list of words. – djcmm476 Nov 02 '12 at 15:33
  • hmm, no matter where I put it it just seems to return the name of the text doc I read in. – djcmm476 Nov 02 '12 at 15:34
  • @Incredidave Even if the first line of your script is `import string` and the second line `print repr(string)`? In that case output should begin with ``. At the place in your script when `print repr(string)` stops printing that line, you've found your bug. If the script doesn't contain an `import string` statement at all, you're dealing with two bugs. – user4815162342 Nov 02 '12 at 15:37
  • @Incredidave: can you not just search for exact token `string`? You question is resolved, we cannot possibly read minds. – SilentGhost Nov 02 '12 at 15:39
  • @user4815162342 I managed to fix it with your advice, thanks a lot. – djcmm476 Nov 02 '12 at 16:50