Python论坛  - 讨论区

标题:[python-chinese] help about readlines

2005年07月21日 星期四 11:09

gyb tension tensiongyb at gmail.com
Thu Jul 21 11:09:46 HKT 2005

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.

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月21日 星期四 11:32

Qiangning Hong hongqn at gmail.com
Thu Jul 21 11:32:25 HKT 2005

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>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月21日 星期四 11:49

gyb tension tensiongyb at gmail.com
Thu Jul 21 11:49:25 HKT 2005

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
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月21日 星期四 13:51

Qiangning Hong hongqn at gmail.com
Thu Jul 21 13:51:45 HKT 2005

因为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>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2005年07月21日 星期四 18:05

gyb tension tensiongyb at gmail.com
Thu Jul 21 18:05:10 HKT 2005

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
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号