zoukankan      html  css  js  c++  java
  • for i in xrange(0,5)使用过程中遇到的问题

    文件中共有4行内容。

    fd = open("C:UserswilliamDesktopdup_file - Copy (2).txt")
    for i in xrange(0,5):
    print type(fd.readline())
    print fd.readline()
    print "-----"

    <type 'str'>
    ccf30309-2a02-4d09-8ea8-70c3da8de09d

    -----
    <type 'str'>
    9cee8472-dfc1-49ac-90c3-572f68373934

    -----
    <type 'str'>

    -----
    <type 'str'>

    -----
    <type 'str'>

    -----

    执行第1个print,返回了第1行的type,然后第2个print,返回了第2行的value,并print后面有换行符

    后面循环,返回了第3行的type, 然后是第4行的value。

    所以这里要注意的是,每次循环,最好注意调用readline的次数。

    fd = open("C:UserswilliamDesktopdup_file - Copy (2).txt")
    for i in xrange(0,5):
    print i
    print fd.readline()
    print "-----"

    0
    8957d983-05e2-4a54-a7b3-f1532421c7a7

    -----
    1
    ccf30309-2a02-4d09-8ea8-70c3da8de09d

    -----
    2
    492ddb07-e0c5-4dda-ae91-8b3700d05a79

    -----
    3
    9cee8472-dfc1-49ac-90c3-572f68373934

    -----
    4

    -----

    fd = open("C:UserswilliamDesktopdup_file - Copy (2).txt")
    for i in xrange(0,5):
    print i
    print fd.readline(),

    0
    8957d983-05e2-4a54-a7b3-f1532421c7a7
    1
    ccf30309-2a02-4d09-8ea8-70c3da8de09d
    2
    492ddb07-e0c5-4dda-ae91-8b3700d05a79
    3
    9cee8472-dfc1-49ac-90c3-572f68373934
    4

    这里主要是复习一下print不换行的方法,后面加逗号","

  • 相关阅读:
    SpringCloud微服务基础学习
    EF6 + MySql 建立项目引用失败
    Forword(请求转发)与Redirect(重定向)区别
    Java 中 Hashtable与HashMap的区别
    cookie和session
    configparser模块的简单使用
    列表中的陷阱
    Python3面向对象编程总结
    Python---RabbitMQ的使用
    Django的template自定义函数的创建和使用
  • 原文地址:https://www.cnblogs.com/guohuino2/p/6042839.html
Copyright © 2011-2022 走看看