【问题标题】:0A hex bug on writing to file?0 写入文件的十六进制错误?
【发布时间】:2010-11-11 12:27:08
【问题描述】:

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

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”是从哪里来的?

【问题讨论】:

    标签: python hex


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-03-16
      • 2013-07-11
      • 2015-08-22
      • 2015-01-03
      • 2011-12-22
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多