0

I am trying to write backwards compatible code that involves reading some data from disk, that includes a UTF-8 string, process it as a string, then write it back to the disk. The only way that work for me is:

 val=fh.read(n) 
 str(val.decode("utf-8")) 

This seems really verbose, but everything else I try fails in either 2.x or 3.x. Is there a neater way to do it?

This will produce a byte array not a string on 3.x

 str(val) 

This fails on 2.x (no second argument)

 str(val,"utf-8") 

This fails when I try and write the value back to disk on 2.x ("expected string, got unicode"):

 val.decode("utf-8")

Tried a couple of combinations but no luck. Is this really the only way to do it?

griffin2000
  • 709
  • 9
  • 26
  • possible duplication: http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string/38102444#38102444 – lmiguelvargasf Dec 06 '16 at 22:57
  • 2
    "process it as a string" - you should process it as whatever type represents Unicode text, not whatever type `str` is. Just use `val.decode('utf-8')` without trying to call `str` on it. – user2357112 Dec 06 '16 at 23:02
  • That has different results on the different platforms. (Sorry pressed enter and it replied too early) – griffin2000 Dec 06 '16 at 23:11
  • This has different results on different versions of python. e.g.: This will work fine on 3.x but will fail on 2.x `stringVal=val.decode('utf-8')` `stringVal=doStuff(stringVal)` `out_fh.write(str.encode(stringVal))` – griffin2000 Dec 06 '16 at 23:32
  • @griffin2000: `str.encode(stringVal)` should be `stringVal.encode('utf-8')`. Also, `doStuff` likely needs to be fixed, if it's anything like the code you've posted so far. – user2357112 Dec 07 '16 at 01:00
  • Possibly a typo (though I think both forms work), but despite that the code won't work on both 2.x and 3.x without the str() call. The contents of doStuff are version agnostic string processing, that should not need to be changed. – griffin2000 Dec 07 '16 at 01:54
  • Actually I am pretty sure stringVal.encode('utf-8') will fail on 2.x (not in front on my computer right now) – griffin2000 Dec 07 '16 at 02:03

0 Answers0