写入文件时是否有0A hex bug?

时间:2010-11-11 12:27:09

标签: python hex

我有一点问题: 为什么这个代码

somefile = open('foo.txt', 'w')
somefile.write('0B0B0B'.decode('hex'))
somefile.close()

在文件中写入0B0B0B,此代码

somefile = open('foo.txt', 'w')
somefile.write('0A0A0A'.decode('hex'))
somefile.close()

在文件中写入0D0A0D0A0D0A? '0D'来自哪里?

1 个答案:

答案 0 :(得分:9)

它来自\n - >由于您在Windows上运行,因此\r\n转换。如果要避免这种情况,请以二进制模式('wb')打开文件。

相关问题