Python:UTF16解码在Windows框上添加了一个新的空白行

2024-10-01 19:31:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个问题,在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. 在Windows和OSX上,将正则表达式编码为utf-16,而不是对文件中断进行解码。

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

有什么想法吗?在


Tags: 文件selfreclosewindowshtmlnix平台
1条回答
网友
1楼 · 发布于 2024-10-01 19:31:33
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

看起来像:

^{pr2}$

(虽然当我把它从记事本粘贴到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位上)

相关问题 更多 >

    热门问题