1

我在 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 上运行此代码可以正常工作 - 我在没有额外换行符的情况下取回了我的文件。到目前为止,我已经尝试过:

  1. 将正则表达式编码为 utf-16 而不是解码文件 - 在 Windows 和 OSX 上中断。

  2. 以 'wb' 模式而不是 'w+' 模式写入 - 在 Windows 上中断。

有任何想法吗?

4

1 回答 1

0
C:\Documents and Settings\Nick>python
ActivePython 2.6.4.10 (ActiveState Software Inc.) based on
Python 2.6.4 (r264:75706, Jan 22 2010, 16:41:54) [MSC v.1500 32 bit (Intel)]...
Type "help", "copyright", "credits" or "license" for more information.
>>> txt = """here
... is all
... my text n stuff."""
>>> f = open('u16.txt','wb')
>>> f.write(txt.encode('utf-16'))
>>> f.close()
>>> exit()

C:\Documents and Settings\Nick>notepad u16.txt

好像:

here is allmy text n stuff.

(尽管当我将它从记事本复制粘贴到 FF 时,它实际上放入了换行符)......但是这个:

C:\Documents and Settings\Nick>
    "C:\Program Files\Windows NT\Accessories\wordpad.exe" u16.txt

好像:

here 
is all
my text n stuff.

(在 Windows XP SP3 32 位上)

于 2010-05-24T13:42:15.093 回答