0A写入文件的十六进制错误? [英] 0A hex bug on writing to file?

查看:234
本文介绍了0A写入文件的十六进制错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a little problem: why does this code

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

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

write 0B0B0B in file, and this code

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

在文件中写入0D0A0D0A0D0A?它来自 \\\

write 0D0A0D0A0D0A in file? Where does that '0D' come from?

推荐答案

由于您在Windows上运行,所以c $ c> - > \r\\\
转换。如果您想避免这种情况,请以二进制模式打开文件('wb')。

It comes from the \n -> \r\n transformation due to the fact that you're running on Windows. Open the file in binary mode ('wb') if you want to avoid this.

这篇关于0A写入文件的十六进制错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆