V2EX = way to explore
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
wapptm
V2EX  ›  Python

Python 新人求助,一模一样的代码在不同文件中运行结果却不同

  •  
  •   wapptm · 2022-05-23 21:49:31 +08:00 · 2541 次点击
    这是一个创建于 674 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import csv
    with open('update.csv',mode='r',encoding='utf-8') as f:
    f_csv=csv.reader(f)
    header=next(f_csv)
    for row in f_csv:
    print ( row )


    上面这段代码是主程序中的一段,执行到 header=next(f_csv) 时就抛出 StopIteration 的错误

    但是如果我把这段代码单独写在一个新的文件中,整个文件只有下面这一段代码,却又能正常运行,不再抛出错误,这个是什么原因?

    import csv

    with open('update.csv',mode='r',encoding='utf-8') as f:
    f_csv=csv.reader(f)
    header=next(f_csv)
    for row in f_csv:
    print(row)
    9 条回复    2022-05-23 23:11:49 +08:00
    ila
        1
    ila  
       2022-05-23 21:51:59 +08:00 via Android
    把文件路径写完整后试试
    wapptm
        2
    wapptm  
    OP
       2022-05-23 22:08:01 +08:00
    试过了,写上完整的路径还是一样报错
    yoghurtoreo
        3
    yoghurtoreo  
       2022-05-23 22:11:18 +08:00
    StopIteration 一般是容器空了,盲猜你 f.read 之类的了👻
    llh880808
        4
    llh880808  
       2022-05-23 22:26:42 +08:00   ❤️ 1
    之前已经读过 f_csv 了,在执行 next(f_csv)之前,f_csv 已经被遍历到最后一个元素,再 next 试图获取下一个就会报错

    类似下面这样(记得空格会被吞,用.代替空格):

    with open('demo.csv') as fr:
    ....for row in fr:
    ........print(row)
    ....next(fr)

    这段示例也会抛出 StopIteration
    wapptm
        5
    wapptm  
    OP
       2022-05-23 22:33:44 +08:00
    那么为什么我把同样的一段代码单独写在一个新的文件中,就不会报错呢?

    import csv

    with open('update.csv',mode='r',encoding='utf-8') as f:
    f_csv=csv.reader(f)
    print(f_csv)
    header=next(f_csv)
    for i in f_csv:
    print(i)
    wapptm
        6
    wapptm  
    OP
       2022-05-23 22:39:16 +08:00
    这两段代码是一样的,运行结果却不一样,唯一能解释的就是运行出错的这个文件中的相关代码导致了此类问题

    在这段代码之前有这么一段代码,我想应该是导致出现此类问题的原因所在:
    f=open('update.csv',mode='a',encoding='utf-8',newline='')
    csv_writer=csv.DictWriter(f,fieldnames=['title','version','href'])
    csv_writer.writeheader()#写入表头

    items=driver.find_elements(By.CSS_SELECTOR,'.structItem-cell:nth-child(2)')
    for item in items:
    #获取子网页链接地址
    href=item.find_element(By.CSS_SELECTOR,'.structItem-title a').get_attribute('href')
    #获取子网页标题
    title=item.find_element(By.CSS_SELECTOR,'.structItem-title a').text
    try:
    #获取子网页版本号,如果无版本号就返回空值
    item.find_element(By.CSS_SELECTOR,'.structItem-title span')
    version=item.find_element(By.CSS_SELECTOR,'.structItem-title span').text
    except:
    version='null'
    dict={
    'title':title,
    'version': version,
    'href': href,
    }
    print(dict)
    csv_writer.writerow(dict)


    with open('update.csv',mode='r',encoding='utf-8') as f:
    f_csv=csv.reader(f)
    print(f_csv)
    header=next(f_csv)
    for i in f_csv:
    print(i)
    arischow
        7
    arischow  
       2022-05-23 22:45:18 +08:00
    如果你只是想跳过 header ,不应该用 next 。

    再简单点说,不要用 next ,用别的方法写就可以绕过了。

    如果你一定要用的话,请看文档: https://docs.python.org/3/library/functions.html#next
    wapptm
        8
    wapptm  
    OP
       2022-05-23 22:47:31 +08:00
    多谢 4 楼的解释
    我找到问题所在了,之前 CSV 文件写入完成后,没有关闭,导致在后面执行 next ()函数时,实际上已经执行到最后一条记录了
    在 循环体后加了个 f.close()就解决这个问题了
    tony9413
        9
    tony9413  
       2022-05-23 23:11:49 +08:00
    不建议这样写,容易出问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3933 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:27 · PVG 18:27 · LAX 03:27 · JFK 06:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.