Seite 1 von 1

Was ist das da in meinem Dictionary?

Verfasst: Donnerstag 9. Juni 2005, 15:46
von MKK
Hi!
Was machen dise "u" plötzlich in meinem Dictionary??

a.getvalue() liefert:

Code: Alles auswählen

a {
    text-decoration: underline;
    font-size: 10px;
    }

okay, nun der Code:

Code: Alles auswählen

a=StringIO.StringIO(cssParse(css)) 
for line in a:
  handleLine(line) #Handles CSS 
okay, nun die funktionen

Code: Alles auswählen

cssDict = {}
filename=""
inner = 0  # true when handling a tag
currtag='' # name of current tag
attrs = '' # list of names within current tag
def handleLine(l):
  global inner, currtag , attrs	
  l = l.split()
  if ('{' in l): # from now on "within '{' brackets"
    inner=1
    currtag = l[0]
    attrs = ""
  elif ('}' in l):  # "leaving the '{}' brackets"
    inner=0
    addToDict(cssDict,currtag,attrs)
    currtag=''
  elif (inner==1):
    attrs = attrs+" ".join(l)
   
def addToDict(dict, name, format): #dict=Dictionary, name=CSS Identifier, format=CSS Formatinfo
  dict.setdefault(name, format)
print cssDict liefert

Code: Alles auswählen

{u'a': u'text-decoration: underline;font-size: 10px;'}
WTF machen diese komischen "u" in meinem Dictionary????

Verfasst: Donnerstag 9. Juni 2005, 17:46
von Leonidas
Das heißt einfach nur, dass die Strings in Unicode sind.

Code: Alles auswählen

>>> s = "Leonidas"
>>> u = u"Leonidas"
>>> type(s)
>>> type(u)
>>> su = str(u)
>>> type(su)
Mehr Infos zu diesem (komplexen) Thema gibt es in den exzellenten Python Unicode FAQ.