for line in open(*)里面怎么倒回去读上一行? - V2EX
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ThunderEX

for line in open(*)里面怎么倒回去读上一行?

  •  
  •   ThunderEX Dec 17, 2012 7640 views
    This topic created in 4894 days ago, the information mentioned may be changed or developed.
    弱智问题一条,for line in open(*)里面一行一行的读文件,但其中突然想临时引用一下前一行的内容,该怎么引用捏?
    13 replies    1970-01-01 08:00:00 +08:00
    CoX
        1
    CoX  
       Dec 17, 2012
    搞个中间变量存一下
    ritksm
        2
    ritksm  
       Dec 17, 2012   1
    .seek(tell()-1)?....
    ritksm
        3
    ritksm  
       Dec 17, 2012
    不过中间变量好像方便些...
    ritksm
        4
    ritksm  
       Dec 17, 2012   1
    不对。。。忽略第一条。。。那个貌似是offset的不是行- -
    ThunderEX
        5
    ThunderEX  
    OP
       Dec 17, 2012
    @ritksm 送铜币……但是还是好奇有没有直接能引用回去的办法~
    unfurl
        6
    unfurl  
       Dec 17, 2012
    for n, line in enumerate(open(file, 'r').readlines())
    likuku
        7
    likuku  
       Dec 17, 2012   1
    open()打开的文件对象没找到index属性,但可以转换为list对象后就可以使用index()来获得当前元素对应的index,当然也可以直接使用list[index]来访问当前元素,或者list[index-1]来访问前一个元素.

    line_src = open ("t")
    line_list = line_src.readlines()
    for line in line_list:
    index_currnet_line = line_list.index(line)
    if index_currnet_line > 0:
    last_line = line_list[index_currnet_line-1]
    chemhack
        8
    chemhack  
       Dec 17, 2012
    @likuku O(1)的操作活活被你搞成了O(N),内存占用也成了O(N)
    ThunderEX
        9
    ThunderEX  
    OP
       Dec 17, 2012
    @likuku 谢~这么看来还是中间变量好了……
    geekard
        10
    geekard  
       Dec 17, 2012
    for line in open(*),这里的open()生成的是匿名文件对象,没法直接引用,能获得的就是下一行内容。即使有文件对象f,还需要知道上一行的字符数,这样才能seek()去。
    ivenvd
        11
    ivenvd  
       Dec 17, 2012
    记得 open(*) 在这种语境已经返回迭代器而不是文件对象了,所以只能中间变量了吧。
    xuwenbao
        12
    xuwenbao  
       Dec 18, 2012
    >>> import linecache
    >>> linecache.getline('/etc/passwd', 4)
    'sys:x:3:3:sys:/dev:/bin/sh\n'
    sykp241095
        13
    sykp241095  
       Dec 18, 2012
    请问iterator支持回退么。
    About     Help     Advertise     Blog     API     FAQ     Solana     2844 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 54ms UTC 15:34 PVG 23:34 LAX 08:34 JFK 11:34
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86