Unicode issue with Python v3.3

This is the place for queries that don't fit in any of the other categories.

Unicode issue with Python v3.3

Postby NikosGr » Tue Apr 09, 2013 9:11 pm

Hello, iam still trying to alter the code form python 2.6 => 3.3

Everyrging its setup except that unicode error that you can see if you go to http://superhost.gr

Can anyone help with this?
I even tried to change print() with sys.stdout.buffer() but still i get the same unicode issue.

I don't know what to try anymore.
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Unicode issue with Python v3.3

Postby stranac » Tue Apr 09, 2013 9:29 pm

There is no 'unicode error' there, but I'm going to guess that you expect to see some greek letters(can't paste them here, there seem to be some mysql db collation issues) instead of \xce\xa3\xce\xb5\xce\xbb\xce\xaf\xce\xb4\xce\xb5\xcf\x82 \xcf\x80\xce\xbf\xcf\x85 \xcf\x86\xce\xb9\xce\xbb\xce\xbf\xce\xbe\xce\xb5\xce\xbd\xce\xbf\xcf\x8d\xce\xbd\xcf\x84\xce\xb1\xce\xb9.

If I remember correctly, you're reading these characters from a file, right?
Show us the file.
Also, show the code you're using to read and display this.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Unicode issue with Python v3.3

Postby NikosGr » Wed Apr 10, 2013 3:22 am

Code: Select all
# pick a random mp3 from the the music directory
quote = random.choice( list( open( "/home/nikos/www/data/private/quotes.txt", encoding="utf-8" ) ) )
# pick a random line from the quote text file
music = random.choice( os.listdir( "/home/nikos/www/data/private/music" ) )

if cur.rowcount:
   if page.endswith('.html'):
      f = open( "/home/nikos/www/" + page, encoding="utf-8" )
      htmldata = f.read()
      htmldata = htmldata % (quote, music)
      
      counter = ''' <center>
                 <a href="mailto:support@superhost.gr"> <img src="/data/images/mail.png"></a>
                 <table border=2 cellpadding=2 bgcolor=black>
                     <td><font color=lime>No of visitors</td>
                     <td><a href="http://superhost.gr/?show=log&page=%s"><font color=yellow> %d </td>
                 </table><br>
           ''' % (page, data[0])
          
      template = htmldata + counter
      print( template )
   elif page.endswith('.py'):
      print( '''<meta http-equiv="REFRESH" content="0;/cgi-bin/%s">''' ) % page
else:
   print( "No one visitor." )



The text file is jus a greek letter utf-8 wthout bom saved .txt
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Unicode issue with Python v3.3

Postby NikosGr » Wed Apr 10, 2013 7:24 am

Code: Select all
Look at what 'python3 metrites.py' gives me

<!-- The above is a description of an error in a Python program, formatted
     for a Web browser because the 'cgitb' module was enabled.  In case you
     are not reading this in a Web browser, here is the original traceback:

Traceback (most recent call last):
  File "metrites.py", line 34, in &lt;module&gt;
    userinfo = os.environ['HTTP_USER_AGENT']
  File "/root/.local/lib/python2.7/lib/python3.3/os.py", line 669, in __getitem__
    value = self._data[self.encodekey(key)]
KeyError: b'HTTP_USER_AGENT'
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Unicode issue with Python v3.3

Postby NikosGr » Wed Apr 10, 2013 8:06 am

Here is the whole code for metrites.py in case someone wants to take allok.

Everything is correct after altering it to meet python 3.3, everythign aprt from the weird unicode error thing.

http://pastebin.com/5Mpjx5Fd

please take a look.
Thank you.
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki

Re: Unicode issue with Python v3.3

Postby stranac » Wed Apr 10, 2013 9:54 am

Sorry, I'm not really able to help you atm.
I could probably figure it out with some testing, but I don't really have the time for that now.

NikosGr wrote:
Code: Select all
Look at what 'python3 metrites.py' gives me

<!-- The above is a description of an error in a Python program, formatted
     for a Web browser because the 'cgitb' module was enabled.  In case you
     are not reading this in a Web browser, here is the original traceback:

Traceback (most recent call last):
  File "metrites.py", line 34, in &lt;module&gt;
    userinfo = os.environ['HTTP_USER_AGENT']
  File "/root/.local/lib/python2.7/lib/python3.3/os.py", line 669, in __getitem__
    value = self._data[self.encodekey(key)]
KeyError: b'HTTP_USER_AGENT'

This is expected, as os.environ['HTTP_USER_AGENT'] is only set when running from browser.
You could fake it:
Code: Select all
userinfo = os.environ.get('HTTP_USER_AGENT', 'some default')
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Unicode issue with Python v3.3

Postby NikosGr » Wed Apr 10, 2013 10:52 am

Thank you just altered it but i still get the same encoding issues.

please its only a matter of simple alternation that iam not able to see.

When you have the time plz take a look.

Thank you!
NikosGr
 
Posts: 48
Joined: Thu Mar 28, 2013 6:31 pm
Location: Thessaloniki


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests