我在 windows 和 *nix 平台上遇到了额外换行符的问题。
file = open('UTF16file.xml', 'rb')
html = file.read().decode('utf-16')
file.close()
regexp = re.compile(self.originalurl, re.S)
(html, changes) = regexp.subn(self.newurl, html)
file = open('UTF16file-regexed.xml', 'w+')
file.write(html.encode('utf-16'))
file.close()
在我的 Mac 上运行此代码可以正常工作 - 我在没有额外换行符的情况下取回了我的文件。到目前为止,我已经尝试过:
将正则表达式编码为 utf-16 而不是解码文件 - 在 Windows 和 OSX 上中断。
以 'wb' 模式而不是 'w+' 模式写入 - 在 Windows 上中断。
有任何想法吗?