python codecs,Python 2.7中的Open()和codecs.open()的行为有着奇怪的不同

I have a text file with first line of unicode characters and all other lines in ASCII.

I try to read the first line as one variable, and all other lines as another. However, when I use the following code:

# -*- coding: utf-8 -*-

import codecs

import os

filename = '1.txt'

f = codecs.open(filename, 'r3', encoding='utf-8')

print f

names_f = f.readline().split(' ')

data_f = f.readlines()

print len(names_f)

print len(data_f)

f.close()

print 'And now for something completely differerent:'

g = open(filename, 'r')

names_g = g.readline().split(' ')

print g

data_g = g.readlines()

print len(names_g)

print len(data_g)

g.close()

I get the following output:

28

7

And now for something completely differerent:

28

77

If I don't use readlines(), whole file reads, not only first 7 lines both at codecs.open() and open().

Why does such thing happen?

And why does codecs.open() read file in binary mode, despite the 'r' parameter is added?

解决方案

Because you used .readline() first, the codecs.open() file has filled a linebuffer; the subsequent call to .readlines() returns only the buffered lines.

If you call .readlines() again, the rest of the lines are returned:

>>> f = codecs.open(filename, 'r3', encoding='utf-8')

>>> line = f.readline()

>>> len(f.readlines())

7

>>> len(f.readlines())

71

The work-around is to not mix .readline() and .readlines():

f = codecs.open(filename, 'r3', encoding='utf-8')

data_f = f.readlines()

names_f = data_f.pop(0).split(' ') # take the first line.

This behaviour is really a bug; the Python devs are aware of it, see issue 8260.

The other option is to use io.open() instead of codecs.open(); the io library is what Python 3 uses to implement the built-in open() function and is a lot more robust and versatile than the codecs module.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值