zoukankan      html  css  js  c++  java
  • Python文件基础

    ===========Python文件基础=========

    写,先写在了IO buffer了,所以要及时保存 关闭。关闭会自动保存。

    file.close()
    读取全部文件内容用read,读取一行用readline—>string(如果是循环,下次读取下一行),读取多行readlines—>list(也是全部读完)

     

    ========Python文件的格式化写入=======

    am001=open('Amfomat.txt','w')

    s='%10s%10s%10s '%('Id','Name','Record')

    am001.write(s)

    s1='%10d%10s%10.2f '%(854179353,'amily',99.8)

    am001.write(s1)

    s2='%10d%10s%10.2f '%(347634734,'yiggle',98.8)

    am001.write(s2)

    s3='%10d%10s%10.2f '%(347634734,'yiggle',98.8)

    am001.write(s3)

    s4='%10d%10s%10.2f '%(347634734,'yiggle',98.8)

    am001.write(s4)

    s5='%10d%10s%10.2f '%(347634734,'yiggle',98.8)

    am001.write(s5)

    s6='%10d%10s%10.2f '%(347634734,'yiggle',98.8)

    am001.write(s6)

    am001.close()

    ============循环体和文件=======

    >>> help(file.readline)

    readline(...)

        readline([size]) -> next line from the file, as a string.

       

        Retain newline.  A non-negative size argument limits the maximum

        number of bytes to return (an incomplete line may be returned then).

    Return an empty string at EOF.

    在linux下退出python环境 可以用quit()

    >>> id1=open('Amfomat.txt','r')

    >>> print id1.readline().strip(' ')

            Id      Name    Record

    >>> print id1.readline().strip(' ')                       #第二次调用就自动取了下一行

     854179353     amily     99.80

    >>> print id1.readline().strip(' ')

     347634734    yiggle     98.80

    ===============用while和readline循环输出文件中所有内容=========

    fw=open('Amfomat.txt','r')

    s=fw.readline()

    while s!='':

            s=s.rstrip(' ')

            print s

            s=fw.readline()

    fw.close()

    ==================for和file_obj=======================

    fr=open('Amfomat.txt','r')

    for str in fr:

       str=str.strip(' ')

       print str

    fr.close()

    这跟上面的遍历输出文件内容效果是一样的。

    s=’www.baidu.com’

    for str1 in s:

       print str1,

    结果为 w w w . b a i d u . c o m

    >>> s='www.baidu.com'

    >>> si=iter(s)                   #iter(xxx),获取xxx的迭代器,strlist以及tuple都可以用迭代器,因此也都可以用for语句

    >>> si.next()                  是从第一个开始的

    'w'

    >>> si.next()

    'w'

    >>> si.next()

    'w'

    >>> si.next()

    '.'

    i = iter('abcd')
    print i.next()
    print i.next()
    print i.next()

    s = {'one':1,'two':2,'three':3}
    print s
    m = iter(s)
    print m.next()
    print m.next()
    print m.next()

    结果

    a

    b

    c

    {'three': 3, 'two': 2, 'one': 1}

    three

    two

    one

    只要某一个对象是一个有序的聚合对象,就可以通过迭代器依次取出它的每个元素,因此也都可以用for语句,字符串,列表,元组,字典都是可以的。到最后一个值得时候抛出异常:stopiteration

    for是通过获取对象的迭代器,然后在通过迭代器里的next方法依次取出迭代器里的各个元素。

    对于文件 ,文件中的每一行就是这个文件局和对象的元素。

    fr=open(‘for.py’,’r’)

    fi=iter(fr)

    print fi.next()

    print fi.next()

    print fi.next()

    type(fr)--àfile

  • 相关阅读:
    新闻资讯APP开发流程(一)需求分析
    药品查询APP开发流程(九)开发—yao_company.js
    药品查询APP开发流程(十一)开发—company_specs.js
    开发demo药品查询
    药品查询APP开发流程(十)开发—company.js
    药品查询APP开发流程(八)开发—yao_specs.js
    开发demo新闻资讯
    新闻资讯APP开发流程(三) app.js
    新闻资讯APP开发流程(二) APP模型设计
    药品查询APP开发流程(七)开发—yao.js
  • 原文地址:https://www.cnblogs.com/AmilyWilly/p/5103492.html
Copyright © 2011-2022 走看看