Python论坛  - 讨论区

标题:答复: 垃圾邮件:Re: [python-chinese] 读取超大文件的最后一行或者多行

2005年12月29日 星期四 09:28

Du Jun jdu at haiercct.com.cn
Thu Dec 29 09:28:42 HKT 2005

fsock = file(filename, 'rU')


你这个U参数是什么作用啊?查了一下,说是:
Add a 'U' to mode to open the file for input with universal newline
不明白什么意思,
 
还有
'U' cannot be combined with 'w' or '+' mode.
为什么?
没用过,请教。
 
-----邮件原件-----
发件人: python-chinese-bounces at lists.python.cn [mailto:python-chinese-bounces at lists.python.cn] 代表 Kevin Yuan
发送时间: 2005年12月28日 16:18
收件人: python-chinese at lists.python.cn
主题: 垃圾邮件:Re: [python-chinese] 读取超大文件的最后一行或者多行
 
#last lines
def last_lines(filename, lines = 1):
    #print the last several line(s) of a text file
    """
    Argument filename is the name of the file to print.
    Argument lines is the number of lines to print from last. 
    """
    block_size = 1024
    block = ''
    nl_count = 0
    start = 0
    fsock = file(filename, 'rU')
    try:
        #seek to end
        fsock.seek(0, 2)
        #get seek position 
        curpos = fsock.tell()
        while(curpos > 0): #while not BOF
            #seek ahead block_size+the length of last read block
            curpos -= (block_size + len(block));
            if curpos < 0: curpos = 0 
            fsock.seek(curpos)
            #read to end
            block = fsock.read()
            nl_count = block.count('\n')
            #if read enough(more)
            if nl_count >= lines: break 
        #get the exact start position
        for n in range(nl_count-lines+1):
            start = block.find('\n', start)+1 
    finally:        
        fsock.close()
    #print it out  
    print block[start:] 
    
if __name__ == '__main__':
    import sys
    last_lines(sys.argv[0], 5) #print the last 5 lines of THIS file
在05-12-28,Vincent Wen < vincentwen at gmail.com> 写道:
python 也可以seek啊,seek到文件尾,然后从后往前找回车符 


在 05-12-27,hoxide Ma<hoxide at gmail.com> 写道:
> 用mmap
>
> 在 05-12-27,bu shehui<bushehui at gmail.com> 写道:
> > If you use Linux, you can use the command such as
> >
> >           tail -n 1  #the last  line
> >
> >
> > good luck
> >
> > 2005-12-27
> > 
> >
> >
> > On 12/22/05, Weigang LI <dimens at gmail.com> wrote:
> > >
> > >
> > > 各位好,
> > > 请问用什么样的方法读取一个超大文件的最后一行,或者文件末尾的n行。 
> > > 由于文件非常大,顺序读取非常的耗时,怎样实现效率高?
> > >
> > > 谢谢。
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to
> > python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to 
> > python-chinese-request at lists.python.cn
> > > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese 
> > >
> > >
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to 
> > python-chinese-request at lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese 
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to   python-chinese-request at lists.python.cn>  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>

_______________________________________________ 
python-chinese
Post: send python-chinese at lists.python.cn
Subscribe: send subscribe to python-chinese-request at lists.python.cn 
Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20051229/ca1c1c60/attachment.html

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

2005年12月29日 星期四 09:49

Kevin Yuan farproc at gmail.com
Thu Dec 29 09:49:06 HKT 2005

Add a 'U' to mode to open the file for input with universal newline
翻译过来就是说"打开文件时为模式添加U可以在输入时使用通用的换行符"。也就是说,添加了U之后,不管要打开的文本文件中的换行符是\n,\r\n,还是\r,在read的时候都会转换为pythn的
标准换行符('\n')。这样方便后续处理。
而在写文件(w,+)的时候,必须使用python的标准换行符\n。所以也就无所谓"通用换行符"了。

在05-12-29,Du Jun <jdu at haiercct.com.cn> 写道:
>
>  fsock = file(filename, 'rU')
>
>  你这个U参数是什么作用啊?查了一下,说是:
>
> Add a 'U' to mode to open the file for input with universal newline
>
> 不明白什么意思,
>
>
>
> 还有
>
> 'U' cannot be combined with 'w' or '+' mode.
> 为什么?
>
> 没用过,请教。
>
>
>
> -----邮件原件-----
> *发件人:* python-chinese-bounces at lists.python.cn [mailto:
> python-chinese-bounces at lists.python.cn] *代表 *Kevin Yuan
> *发送时间:* 2005年12月28日 16:18
> *收件人:* python-chinese at lists.python.cn
> *主题:* 垃圾邮件:Re: [python-chinese] 读取超大文件的最后一行或者多行
>
>
>
> #last lines
> def last_lines(filename, lines = 1):
>     #print the last several line(s) of a text file
>     """
>     Argument filename is the name of the file to print.
>     Argument lines is the number of lines to print from last.
>     """
>     block_size = 1024
>     block = ''
>     nl_count = 0
>     start = 0
>     fsock = file(filename, 'rU')
>     try:
>         #seek to end
>         fsock.seek(0, 2)
>         #get seek position
>         curpos = fsock.tell()
>         while(curpos > 0): #while not BOF
>             #seek ahead block_size+the length of last read block
>             curpos -= (block_size + len(block));
>             if curpos < 0: curpos = 0
>             fsock.seek(curpos)
>             #read to end
>             block = fsock.read()
>             nl_count = block.count('\n')
>             #if read enough(more)
>             if nl_count >= lines: break
>         #get the exact start position
>         for n in range(nl_count-lines+1):
>             start = block.find('\n', start)+1
>     finally:
>         fsock.close()
>     #print it out
>     print block[start:]
>
> if __name__ == '__main__':
>     import sys
>     last_lines(sys.argv[0], 5) #print the last 5 lines of THIS file
>
> 在05-12-28,*Vincent Wen* < vincentwen at gmail.com> 写道:
>
> python 也可以seek啊,seek到文件尾,然后从后往前找回车符
>
>
> 在 05-12-27,hoxide Ma<hoxide at gmail.com> 写道:
> > 用mmap
> >
> > 在 05-12-27,bu shehui<bushehui at gmail.com> 写道:
> > > If you use Linux, you can use the command such as
> > >
> > >           tail -n 1  #the last  line
> > >
> > >
> > > good luck
> > >
> > > 2005-12-27
> > >
> > >
> > >
> > > On 12/22/05, Weigang LI <dimens at gmail.com> wrote:
> > > >
> > > >
> > > > 各位好,
> > > > 请问用什么样的方法读取一个超大文件的最后一行,或者文件末尾的n行。
> > > > 由于文件非常大,顺序读取非常的耗时,怎样实现效率高?
> > > >
> > > > 谢谢。
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to
> > > python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > > Detail Info:
> > > http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > >
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to
> > > python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info:
> > > http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20051229/b6edb691/attachment.htm

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号