zoukankan      html  css  js  c++  java
  • 文件操作练习.

    1.有如下文件,a1.txt,里面的内容为:
    LNH是最好的培训机构,
    全心全意为学生服务,
    只为学生未来,不为牟利。
    我说的都是真的。哈哈
    分别完成以下的功能:
    a,将原文件全部读出来并打印。
    b,在原文件后面追加一行内容:信不信由你,反正我信了。
    c,将原文件全部读出来,并在后面添加一行内容:信不信由你,反正我信了。
    d,将原文件全部清空,换成下面的内容:
    每天坚持一点,
    每天努力一点,
    每天多思考一点,
    慢慢你会发现,
    你的进步越来越大。
    e,将原文件内容全部读取出来,并在‘我说的都是真的。哈哈’这一行的前面加一行,
    ‘你们就信吧~’然后将更改之后的新内容,写入到一个新文件:a1.txt。

    a.f = open("al.txt",mode="r",encoding="utf-8")
    s = f.read()
    f.close()
    print(s)

    b.f = open("al.txt",mode="a",encoding="utf-8")
    f.write(' 信不信由你,反正我信了。')
    f.flush()
    f.close()

    c.f = open("al.txt",mode="r+",encoding="utf-8")
    s = f.read()
    print(s)
    f.write(' 信不信由你,反正我信了。')
    f.flush()
    f.close()

    d.f = open("al.txt",mode="w",encoding="utf-8")
    s = f.write('每天坚持一点, 每天努力一点, 每天多思考一点, 慢慢你会发现, 你的进步越来越大。')
    print(s)
    f.close()

    e.import os
    with open("a1.txt",mode='r',encoding='utf-8') as f1 ,open("a1(1).txt",mode='w',encoding='utf-8') as f2:
    s = f1.read()
    ss = s.replace('我说的都是真的。哈哈','你们就信吧~ 我说的都是真的。哈哈')
    f2.write(ss)
    os.remove('a1.txt')
    os.rename('a1(1).txt','a1.txt')

    2.有如下文件,t1.txt,里面的内容为:
    葫芦娃,葫芦娃,
    一根藤上七个瓜
    风吹雨打,都不怕,
    啦啦啦啦。
    我可以算命,而且算的特别准:
    上面的内容你肯定是心里默唱出来的,对不对?哈哈
    分别完成下面的功能:
    a,以r+的模式打开原文件,判断原文件是否可读,是否可写。
    b,以r的模式打开原文件,利用for循环遍历文件句柄。
    c,以r的模式打开原文件,以readlines()方法读取出来,并循环遍历readlines(),并分析b与c有什么区别?深入理解文件句柄与readlines()结果的区别。
    d,以r模式读取‘葫芦娃,’前四个字符。
    e,以r模式读取第一行内容,并去除此行前后的空格,制表符,换行符。
    f,以r模式打开文件,从‘风吹雨打.....’开始读取,一直读到最后。
    g,以a+模式打开文件,先追加一行:‘老男孩教育’然后在从最开始将原内容全部读取出来。
    h,截取原文件,截取内容:‘葫芦娃,葫芦娃,’

    a.f = open("t1.txt",mode='r+',encoding='utf-8')
    print(f.readable())
    print(f.writable())
    f.close()

    b.f = open("t1.txt",mode='r',encoding='utf-8')
    for a in f:
    print(a)
    f.close()

    c.f = open("t1.txt",mode='r',encoding='utf-8')
    s = f.readlines()
    for line in s:
    print(line)
    f.close()

    d.f = open("t1.txt",mode='r',encoding='utf-8')
    s = f.read(4)
    print(s)
    f.close()

    e.f = open("t1.txt",mode='r',encoding='utf-8')
    s = f.readline()
    print(s.strip())
    f.close()

    f.f = open("t1.txt",mode='r',encoding='utf-8')
    for line in f:
    if line.startswith('风吹雨打'):
    print(line, end='')
    print(f.read())
    f.close()

    g.f = open("t1.txt",mode='a+',encoding='utf-8')
    s = f.write(' 老男孩教育')
    f.seek(0)
    print(f.read())
    f.close()

    h.f = open("t1.txt",mode="r+",encoding='utf-8')
    f.seek(24)
    f.truncate()
    f.seek(0)
    s = f.read()
    print(s)
    f.close()

    3.文件a.txt内容:每一行内容分别为商品名字,价钱,个数。
    apple 10 3
    tesla 100000 1
    mac 3000 2
    lenovo 30000 3
    chicken 10 3
    通过代码,将其构建成这种数据类型:
    [{'name':'apple','price':10,'amount':3},{'name':'tesla','price':1000000,'amount':1}......]
    并计算出总价钱。
    f = open('a.txt',mode='r',encoding='utf-8')
    lst = []
    sum = 0
    for a in f:
    dic = {}
    s = a.split(' ')
    dic['name'] = s[0]
    dic['price'] = int(s[1])
    dic['amount'] = int(s[2])
    sum += int(s[1])*int(s[2])
    lst.append(dic)
    print(lst)
    print(sum)

    4.有如下文件:

    alex是老男孩python发起人,创建人。
    alex其实是人妖。
    谁说alex是sb?
    你们真逗,alex再牛逼,也掩饰不住资深屌丝的气质。

    将文件中所有的alex都替换成大写的SB(文件的改的操作)。
    import os
    with open('a.txt',mode='r',encoding='utf-8') as f1,open('a1.txt',mode='w',encoding='utf-8') as f2:
    s = f1.read()
    ss = s.replace('alex','SB')
    f2.write(ss)
    os.remove('a.txt')
    os.rename('a1.txt','a.txt')

    5.文件a1.txt内容(升级题)
    name:apple price:10 amount:3 year:2012
    name:tesla price:100000 amount:1 year:2013
    .......
    通过代码,将其构建成这种数据类型:
    [{'name':'apple','price':10,'amount':3},
    {'name':'tesla','price':1000000,'amount':1}......]
    并计算出总价钱。
    f = open('a1.txt',mode='r',encoding='utf-8')
    lst = []
    sum = 0
    for a in f:
    dic = {}
    s = a.strip().split(' ')
    for el in s:
    ss = el.split(':')
    dic[ss[0]] = ss[1]
    sum += int(dic['price'])*int(dic['amount'])
    lst.append(dic)
    print(lst)
    print(sum)

    6.文件a1.txt内容(升级题)
    序号 部门 人数 平均年龄 备注
    1 python 30 26 单身狗
    2 Linux 26 30 没对象
    3 运营部 20 24 女生多
    .......
    通过代码,将其构建成这种数据类型:
    [{'序号':'1','部门':Python,'人数':30,'平均年龄':26,'备注':'单身狗'},
    ......]
    f = open('a1.txt',mode='r',encoding='utf-8')
    sh = f.readline()
    qg1 = sh.split()
    jg = []
    for line in f:
    ll = line.split()
    dic = {}
    for el in range(len(ll)):
    dic[qg1[el]] = ll[el]
    jg.append(dic)
    print(jg)
  • 相关阅读:
    AtCoder Beginner Contest 205
    Codeforces Round #725 (Div. 3)
    Educational Codeforces Round 110 (Rated for Div. 2)【A
    Codeforces Round #722 (Div. 2)
    AtCoder Beginner Contest 203(Sponsored by Panasonic)
    AISing Programming Contest 2021(AtCoder Beginner Contest 202)
    PTA 520 钻石争霸赛 2021
    Educational Codeforces Round 109 (Rated for Div. 2)【ABCD】
    AtCoder Beginner Contest 200 E
    Educational Codeforces Round 108 (Rated for Div. 2)【ABCD】
  • 原文地址:https://www.cnblogs.com/zjx1/p/10559720.html
Copyright © 2011-2022 走看看