我有一点问题: 为什么这个代码
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'来自哪里?
答案 0 :(得分:9)
它来自\n
- >由于您在Windows上运行,因此\r\n
转换。如果要避免这种情况,请以二进制模式('wb'
)打开文件。