Write different hex-values in Python2 and Python3

前端 未结 3 1566
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 01:29

I\'m currently porting a Python2 script to Python3 and have problems with this line:

print(\'\\xfe\')

When I run it with Python2 pyth

3条回答
  •  鱼传尺愫
    2020-12-21 01:55

    The print(argument) converts the argument using str() (if neccessary) and then it calls file.write(string). The file is the optional argument of the print(), and it defaults to sys.stdout. It means that you should be able to do the same with sys.stdout.write(str(argument) + '\n'). So, the result depends on the used encoding that you can get from sys.stdout.encoding. If you pass another file argument, then the file object have to be open for writing in the text mode, and possibly a different encoding may be applied.

提交回复
热议问题