2005年07月21日 星期四 11:09
The instruction of readlines readlines(...) | readlines([size]) -> list of strings, each a line from the file. | | Call readline() repeatedly and return a list of the lines so read. | The optional size argument, if given, is an approximate bound on the | total number of bytes in the lines returned. What does size argument mean?I don't understand. readlines function: - def readlines(self, sizehint=0): + def readlines(self, size=-1): + """readlines([size]) -> list of strings, each a line from the file. + +Call readline() repeatedly and return a list of the lines so read. +The optional size argument, if given, is an approximate bound on the +total number of bytes in the lines returned.""" if self._closed: raise ValueError('I/O operation on closed file') - return list(iter(self.stream.readline, "")) + if size < 0: + return list(iter(self.stream.readline, "")) + else: + result = [] + while size > 0: + line = self.stream.readline() + if not line: + break + result.append(line) + size -= len(line) + return result why does my result does match with it? for example: f.readlines(1) returns ['111111','222222222','3333333','44444444'... ...].(don't list all) f.readlines(1000) returns only 68 lines. why? Help me please.
2005年07月21日 星期四 11:32
gyb tension wrote: > The instruction of readlines > readlines(...) > | readlines([size]) -> list of strings, each a line from the file. > | > | Call readline() repeatedly and return a list of the lines so read. > | The optional size argument, if given, is an approximate bound on the > | total number of bytes in the lines returned. > > What does size argument mean?I don't understand. docstring里写的很明白啊,size是对返回的行的总字节数的一个估计值。 [snip readlines source code] > > why does my result does match with it? > for example: > f.readlines(1) returns ['111111','222222222','3333333','44444444'... > ...].(don't list all) > f.readlines(1000) returns only 68 lines. > why? Help me please. 因为这68行刚好超过1000字节。 size的单位是字节数,不是行数。如果你想要返回f的前1000行,应该这样写: [f.readline() for i in xrange(1000)] -- Qiangning Hong I'm usually annoyed by IDEs because, for instance, they don't use VIM as an editor. Since I'm hooked to that, all IDEs I've used so far have failed to impress me. -- Sybren Stuvel @ c.l.python Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
2005年07月21日 星期四 11:49
Thank you! But why does f.readlines(1) returns ['111111','222222222','3333333','44444444'... ...].(don't list all)? On 7/21/05, Qiangning Hong <hongqn at gmail.com> wrote: > gyb tension wrote: > > The instruction of readlines > > readlines(...) > > | readlines([size]) -> list of strings, each a line from the file. > > | > > | Call readline() repeatedly and return a list of the lines so > read. > > | The optional size argument, if given, is an approximate bound on > the > > | total number of bytes in the lines returned. > > > > What does size argument mean?I don't understand. > > docstring里写的很明白啊,size是对返回的行的总字节数的一个估计值。 > > [snip readlines source code] > > > > why does my result does match with it? > > for example: > > f.readlines(1) returns ['111111','222222222','3333333','44444444'... > > ...].(don't list all) > > f.readlines(1000) returns only 68 lines. > > why? Help me please. > > 因为这68行刚好超过1000字节。 > > size的单位是字节数,不是行数。如果你想要返回f的前1000行,应该这样写: > > [f.readline() for i in xrange(1000)] > > > > > -- > Qiangning Hong > > I'm usually annoyed by IDEs because, for instance, they don't use VIM > as an editor. Since I'm hooked to that, all IDEs I've used so far have > failed to impress me. > -- Sybren Stuvel @ c.l.python > > Get Firefox! > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese >
2005年07月21日 星期四 13:51
因为readlines是通过一个不大于8192字节的buffer来读文件的,而不是一行一行 的读,以提高效率。这也是为什么参数为 *approximate* 的原因。 gyb tension wrote: > Thank you! > > But why does f.readlines(1) returns > ['111111','222222222','3333333','44444444'... ...].(don't list all)? > > On 7/21/05, Qiangning Hong <hongqn at gmail.com> wrote: > >>gyb tension wrote: >> >>>The instruction of readlines >>>readlines(...) >>> | readlines([size]) -> list of strings, each a line from the file. >>> | >>> | Call readline() repeatedly and return a list of the lines so >> >>read. >> >>> | The optional size argument, if given, is an approximate bound on >> >>the >> >>> | total number of bytes in the lines returned. >>> >>>What does size argument mean?I don't understand. >> >>docstring里写的很明白啊,size是对返回的行的总字节数的一个估计值。 >> >>[snip readlines source code] >> >>>why does my result does match with it? >>>for example: >>>f.readlines(1) returns ['111111','222222222','3333333','44444444'... >>>...].(don't list all) >>>f.readlines(1000) returns only 68 lines. >>>why? Help me please. >> >>因为这68行刚好超过1000字节。 >> >>size的单位是字节数,不是行数。如果你想要返回f的前1000行,应该这样写: >> >>[f.readline() for i in xrange(1000)] >> >> >> >> >>-- >>Qiangning Hong >> >>I'm usually annoyed by IDEs because, for instance, they don't use VIM >>as an editor. Since I'm hooked to that, all IDEs I've used so far have >>failed to impress me. >> -- Sybren Stuvel @ c.l.python >> >>Get Firefox! >><http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> >>_______________________________________________ >>python-chinese list >>python-chinese at lists.python.cn >>http://python.cn/mailman/listinfo/python-chinese >> >> >> >>------------------------------------------------------------------------ >> >>_______________________________________________ >>python-chinese list >>python-chinese at lists.python.cn >>http://python.cn/mailman/listinfo/python-chinese -- Qiangning Hong I'm usually annoyed by IDEs because, for instance, they don't use VIM as an editor. Since I'm hooked to that, all IDEs I've used so far have failed to impress me. -- Sybren Stuvel @ c.l.python Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
2005年07月21日 星期四 18:05
Thank you very much! I see.It really made me get into trouble. On 7/21/05, Qiangning Hong <hongqn at gmail.com> wrote: > 因为readlines是通过一个不大于8192字节的buffer来读文件的,而不是一行一行 > 的读,以提高效率。这也是为什么参数为 *approximate* 的原因。 > > gyb tension wrote: > > Thank you! > > > > But why does f.readlines(1) returns > > ['111111','222222222','3333333','44444444'... ...].(don't list all)? > > > > On 7/21/05, Qiangning Hong <hongqn at gmail.com> wrote: > > > >>gyb tension wrote: > >> > >>>The instruction of readlines > >>>readlines(...) > >>> | readlines([size]) -> list of strings, each a line from the file. > >>> | > >>> | Call readline() repeatedly and return a list of the lines so > >> > >>read. > >> > >>> | The optional size argument, if given, is an approximate bound on > >> > >>the > >> > >>> | total number of bytes in the lines returned. > >>> > >>>What does size argument mean?I don't understand. > >> > >>docstring里写的很明白啊,size是对返回的行的总字节数的一个估计值。 > >> > >>[snip readlines source code] > >> > >>>why does my result does match with it? > >>>for example: > >>>f.readlines(1) returns ['111111','222222222','3333333','44444444'... > >>>...].(don't list all) > >>>f.readlines(1000) returns only 68 lines. > >>>why? Help me please. > >> > >>因为这68行刚好超过1000字节。 > >> > >>size的单位是字节数,不是行数。如果你想要返回f的前1000行,应该这样写: > >> > >>[f.readline() for i in xrange(1000)] > >> > >> > >> > >> > >>-- > >>Qiangning Hong > >> > >>I'm usually annoyed by IDEs because, for instance, they don't use VIM > >>as an editor. Since I'm hooked to that, all IDEs I've used so far have > >>failed to impress me. > >> -- Sybren Stuvel @ c.l.python > >> > >>Get Firefox! > >><http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > >>_______________________________________________ > >>python-chinese list > >>python-chinese at lists.python.cn > >>http://python.cn/mailman/listinfo/python-chinese > >> > >> > >> > >>------------------------------------------------------------------------ > >> > >>_______________________________________________ > >>python-chinese list > >>python-chinese at lists.python.cn > >>http://python.cn/mailman/listinfo/python-chinese > > > -- > Qiangning Hong > > I'm usually annoyed by IDEs because, for instance, they don't use VIM > as an editor. Since I'm hooked to that, all IDEs I've used so far have > failed to impress me. > -- Sybren Stuvel @ c.l.python > > Get Firefox! > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese >
Zeuux © 2025
京ICP备05028076号