3

I'm hitting a painful error in a Django command I have written, which is parsing a set of data. The script parses the data, builds the ORM object, and tries to save it. When saving, I get the following error - AttributeError: 'Cursor' object has no attribute '_last_executed' My database is set to utf-8

Doing a bunch of research online trying to solve this did not yield much help. The closest I found was this Error: Cursor' object has no attribute '_last_executed

Where a 'fix' was to call unicode() on my strings. Which worked, until I had strings with apostrophes, such as u'aunt\u2019s' - then things quickly fall over.

So the above leads me to believe the initial cursor issue is something weird with encodings. I dont understand encodings enough to really understand the issue.

I've tried playing with the encode, decode calls manually, but no luck.

How should I go about solving this problem? It seems to be a mix of a Django bug and understanding encodings.

Some code snippets that may help

c=urllib2.urlopen(page_url)
soup=BeautifulSoup(c.read())
my_string = soup.find('title')['content']
my_ormObj = Foo()
foo.title = my_string
foo.save()

The above snippet is the kind of flow I'm using, and in some cases yields the error listed above. I try to take my_string and change it, and it gives the issues I listed in my third paragraph.

Community
  • 1
  • 1
Steve
  • 2,108
  • 4
  • 25
  • 36
  • Can you post some code? At least the parts where you do `unicode()`. Also, what encoding are using your strings? – Paulo Bu Jun 10 '13 at 21:04
  • Honestly, there isn't a ton of code to show. I'll update the above with some snippets, not sure if it will help though – Steve Jun 10 '13 at 21:25
  • 1
    What is `my_string` (hint: `repr()`)? – Ignacio Vazquez-Abrams Jun 10 '13 at 21:31
  • What do you mean? And sorry, it should of been soup.find('title')['content'] (fixed above) – Steve Jun 10 '13 at 21:50
  • Paulo - I'm not sure what encoding the strings are - I'd expect they're the default encoding that BeautifulSoup/django return. I never changed it. So I'd expect it's unicode? – Steve Jun 10 '13 at 21:56
  • I answered something I thought logical but it will be helpful if you tell us the line in your script where the exception is raising. – Paulo Bu Jun 10 '13 at 23:12
  • Can i ask if the problem is solved or not ? I'm having exactly the same problem with BeautifulSoup Strings ... – SpiXel Jul 23 '13 at 05:33

1 Answers1

1

Ok so, if it has to do with encodings, then probably you should encode your string to utf-8 before saving it, as is the same encoding of your database.

I suggest you to try this way:

foo.title = my_string.encode('utf-8')
foo.save()

Is fair to assume that my_string is already Unicode because you posted this example: u'aunt\u2019s', which lead me to think you read it in some error dump Django/Python throw.

IMPORTANT: Forget about those unicode() conversions. If the string is with other encoding than ASCII, which they probably are, using that conversion will throw errors.

That is indeed unicode: Right single quotation mark.

I hope this helps!

Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • Thanks for the response. I tried doing what you said previously, and just tried it again to double check. When I do it, I oddly get a decode error -UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 242: ordinal not in range(128) any other ideas? :) – Steve Jun 10 '13 at 23:16
  • That means my_string is not unicode. Seems to me it is utf-8 because 'xe2 is single quotation mark in utf-8 too. With the code exactly the way you have it in your question, where do you get the error? Line? – Paulo Bu Jun 10 '13 at 23:21
  • Here, I had it print out what it was trying to convert - take a look at http://pastebin.com/rMA0hS67 – Steve Jun 10 '13 at 23:29
  • Nice but will be helpful a Traceback of the exception. This exception and the other before including utf-8 encode. – Paulo Bu Jun 10 '13 at 23:31