Returns the given value as a Python byte string (if possible).
(v, encoding="utf-8")
| 73 | return unicode(v) |
| 74 | |
| 75 | def encode_string(v, encoding="utf-8"): |
| 76 | """ Returns the given value as a Python byte string (if possible). |
| 77 | """ |
| 78 | if isinstance(encoding, basestring): |
| 79 | encoding = ((encoding,),) + (("windows-1252",), ("utf-8", "ignore")) |
| 80 | if isinstance(v, unicode): |
| 81 | for e in encoding: |
| 82 | try: return v.encode(*e) |
| 83 | except: |
| 84 | pass |
| 85 | return v |
| 86 | return str(v) |
| 87 | |
| 88 | decode_utf8 = decode_string |
| 89 | encode_utf8 = encode_string |