zoukankan      html  css  js  c++  java
  • python学习日记(文件操作练习题)

    登录注册(三次机会)

    name = input('请注册姓名:')
    password = input('请注册密码:')
    with open('log',mode='w',encoding='utf-8') as f:
        f.write('{}
    {}'.format(name,password))#格式化输出,两个参数写进一行,write不允许两个变量
    print('注册成功')
    lis = []
    count = 3
    while count > 0:
        count -= 1
        with open('log', mode='r+', encoding='utf-8') as f1:
            for i in f1:
                lis.append(i)#读取内容写入到列表中
            name1 = input('请输入登录用户名:')
            if name1 == lis[0].strip():#strip()可以去除
    ,	
                password1 = input('请输入登录密码:')
                if password1 == lis[1]:
                    print('
    登录成功!')
                    break
                else:
                    print('密码错误')
                    print('您还有%d次机会'%(count))
                    if count == 0:
                        chance = input('机会已用完,是否还想继续?Y(case-sensitive)')
                        if chance.upper() == 'Y':
                            count = 3
                            continue
            else:
                print('用户名错误,请重新输入')
                print('您还有%d次机会'%(count))
                if count == 0:
                    chance = input('机会已用完,是否还想继续?Y(case-sensitive)')
                    if chance.upper() == 'Y':
                        count = 3
                        continue
    else:
        print('
    明天再来吧')

     改变文件数据类型

    # 1. 文件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}......] 并计算出总价钱。
    题目

    一、(问题少年)

    with open('a.txt','r+',encoding='utf-8') as f:
        li = []
        li1 = []
        for line in f:
            li.append(line.split())
        print(li)
        # dic = {}#问题在这里,dic只有一个地址(自己遇到的问题 1),最后字典里只有一组值,列表里添加的全都是这一组,内存指向问题
        j = 0
        sum = 0
        while j < len(li):
            dic = {}#解决问题:每次循环都重新建一个新的dic
            dic['name'] = li[j][0]
            dic['price'] = li[j][1]
            dic['amount'] = li[j][2]
            print(dic)
            li1.append(dic)#直接添加字典,必须每次循环都要重新定义字典,另开内存地址,不然一直指向同一个地址,列表添加多少次值都是字典最后更新的数据
            # li1.append({'name':li[j][0],'price':li[j][1],'amount':li[j][2]})
            cost = int(li[j][1]) * int(li[j][2])#不转成int 不能进行运算str
            # cost = int(dic['price']) * int(dic['amount'])#不转成int 不能进行运算str(自己遇到的问题2)
            print(cost)
            sum += cost
            j = j+1
    print(li1)
    print('总的价格为:',sum)#+加号拼接提示int 和 str不能拼接,所以用逗号。(自己遇到的问题3)

    二、(新的角度)

    with open('a.txt','r+',encoding='utf-8') as f:
        li = []
        li1 = []
        for line in f:
            li.append(line.split())
        print(li)
        # dic = {}
        j = 0
        sum = 0
        while j < len(li):
            dic = {}#看到没?放这里啦,关注点一
            dic['name'] = li[j][0]
            dic['price'] = li[j][1]
            dic['amount'] = li[j][2]
            print(dic)
            # li1.append(dic)
            li1.append({'name':li[j][0],'price':li[j][1],'amount':li[j][2]})#从列表添加value,关注点二
            # cost = int(li[j][1]) * int(li[j][2])#不转成int 不能进行运算str
            cost = int(dic['price']) * int(dic['amount'])#不转成int 不能进行运算str
            print(cost)
            sum += cost
            j = j+1
    print(li1)
    print('总的价格为:',sum)#+加号拼接提示int 和 str不能拼接,所以用逗号。

    修改文件内容

    有如下文件:
    -------
    This is a message
    tony是一个名副其实的iron-man。
    tony其实是个男人。
    谁说tony是坏人?
    你们真逗,tony再厉害,也掩饰不住资深富人的气质。
    ----------
    将文件中所有的tony都替换成大写的IM。

    with open('file','r+',encoding='utf-8') as f1,
        open('file.bak','a',encoding='utf-8') as f2:
        for line in f1:
            if 'tony' in line:
                line = line.replace('tony','IM')
            f2.write(line)
    import os
    os.remove('file')
    os.rename('file.bak','file')

    pass

    作者:Gruffalo
    ---------------------------------------------
    天行健,君子以自强不息
    地势坤,君子以厚德载物
    内容仅为自己做日常记录,备忘笔记等
    真小白,努力学习ing...一起加油吧!(ง •̀_•́)ง
  • 相关阅读:
    poj 1328 Radar Installation (贪心)
    hdu 2037 今年暑假不AC (贪心)
    poj 2965 The Pilots Brothers' refrigerator (dfs)
    poj 1753 Flip Game (dfs)
    hdu 2838 Cow Sorting (树状数组)
    hdu 1058 Humble Numbers (DP)
    hdu 1069 Monkey and Banana (DP)
    hdu 1087 Super Jumping! Jumping! Jumping! (DP)
    必须知道的.NET FrameWork
    使用记事本+CSC编译程序
  • 原文地址:https://www.cnblogs.com/smallfoot/p/10009373.html
Copyright © 2011-2022 走看看