Returns the given value as a Unicode string (if possible).
(v, encoding="utf-8")
| 31 | # assigned to these code points. |
| 32 | |
| 33 | def decode_string(v, encoding="utf-8"): |
| 34 | """ Returns the given value as a Unicode string (if possible). |
| 35 | """ |
| 36 | if isinstance(encoding, basestring): |
| 37 | encoding = ((encoding,),) + (("windows-1252",), ("utf-8", "ignore")) |
| 38 | if isinstance(v, str): |
| 39 | for e in encoding: |
| 40 | try: return v.decode(*e) |
| 41 | except: |
| 42 | pass |
| 43 | return v |
| 44 | return unicode(v) |
| 45 | |
| 46 | def encode_string(v, encoding="utf-8"): |
| 47 | """ Returns the given value as a Python byte string (if possible). |