Seite 1 von 1

Escape kodierten String mit Script erzeugen

Verfasst: Dienstag 17. Juli 2007, 09:41
von JR
Hallo!

Ich habe jetzt ewig gesucht und probiert, es klappt nicht.
Ich möchte in Zope zwei Pythonskripte anlegen, welche mir String in die url-kodierte Form einer Escape-Sequenz wandeln und wieder zurückwandeln.

Beispiel:
Ich gebe "Hans Würstchen" hin und das Wandeln in Escape-Sequenz soll
"Hans%20W%FCrstchen" zurückliefern.

Weiß jemand weiter?

Ich habe folgendes probiert, wobei s das Argument des Scripts ist

Code: Alles auswählen

string_escaped = ''
for no, letter in enumerate(s):
    if no % 2 == 1:
    string_escaped += '%' + str(ord(unicode(letter.decode('iso-8859-1')))).upper()

print string_escaped
return printed
Danke und viele Grüße aus Berlin
JR

Re: Escape kodierten String mit Script erzeugen

Verfasst: Dienstag 17. Juli 2007, 09:54
von gerold
JR hat geschrieben:Ich gebe "Hans Würstchen" hin und das Wandeln in Escape-Sequenz soll "Hans%20W%FCrstchen" zurückliefern.
Hallo JR!

Das liefert allerdings nicht explizit "iso-8859-1" zurück. Genügt dir das trotzdem?

Code: Alles auswählen

from Products.PythonScripts.standard import url_quote, url_unquote
quoted = url_quote("Hans Würstchen")
print quoted
print url_unquote(quoted)
return printed
mfg
Gerold
:-)

Verfasst: Dienstag 17. Juli 2007, 10:09
von JR
Hallo Gerold!

Vielen vielen Dank und es klappt so:

Code: Alles auswählen

from Products.PythonScripts.standard import url_quote, url_unquote 
quoted = url_quote(string) 
print quoted 
print context.code_utf_to_iso(url_unquote(quoted))
return printed
mit code_utf_to_iso (Argument: s_utf8)

Code: Alles auswählen

s_iso=[]
if s_utf8:
  for result in s_utf8:
     try:
       s_unicode = s_utf8.decode("utf-8")  
       s_iso = s_unicode.encode("iso-8859-1")
     except:
       pass

  if s_iso != []:
    return s_iso
  else:
    return s_utf8
else:
  return s_utf8
Viele Grüße
JR