Returns the given value as a Python byte string (if possible).
(v, encoding="utf-8")
| 25 | return unicode(v) |
| 26 | |
| 27 | def encode_string(v, encoding="utf-8"): |
| 28 | """ Returns the given value as a Python byte string (if possible). |
| 29 | """ |
| 30 | if isinstance(encoding, basestring): |
| 31 | encoding = ((encoding,),) + (("windows-1252",), ("utf-8", "ignore")) |
| 32 | if isinstance(v, unicode): |
| 33 | for e in encoding: |
| 34 | try: return v.encode(*e) |
| 35 | except: |
| 36 | pass |
| 37 | return v |
| 38 | return str(v) |
| 39 | |
| 40 | decode_utf8 = decode_string |
| 41 | encode_utf8 = encode_string |