没搞明白fileinput模块的inplace!

__author__ = ‘sophi‘
import fileinput
product_list = []
f = open("porduct.txt","r",encoding="utf-8") #打开文件
for line in f.readlines():
    line = line.strip()
    index,item = line.split(":") #冒号分割
    product_list.append((index,item))
print(product_list) #存入列表
f.close()

def print_product_list():
    for index,item in enumerate(product_list):
        print(index,item)

def user_shoping():
    salary = input("请你输入你的薪水:")
#    print_product_list()
    if salary.isdigit(): #判断输入的是不是数字
        salary = int(salary)

        print_product_list()
        shopping_list = [] #存放购买的商品,购物清单
        while True:
            option = input("喜欢那个就买那个(对应的标号):")
            if option.isdigit():
                option = int(option)
                if option >= 0 and option < len(product_list):
                    p_item = product_list[option] #用户选择的商品 列表
                    print("退出请输入q或Q。")
                    c_num = int(p_item[1])
                    if salary >= c_num:
                        shopping_list.append(p_item)
                        salary -= c_num
                        print("添加购物车成功,你的余额还有%s" %(salary))
                    else:
                        print("你的余额不足,只剩下%s元" %(salary))
                else:
                    print("输入错误,请重新输入")
                    print_product_list()
            elif option == "q":
                print("-------------您的购物清单---------------")
                for s_list in shopping_list:
                    print(s_list)
                print("你的余额为%s" %salary)
                print("........exit.........")
                exit()
            else:
                print("你输入的薪水无效,请重新输入")

#print(len(product_list))
#user_shoping() #用户入口

def add_product():
    print_product_list() #打印一下以前的列表
    name_of_product = input("请输入你要添加的商品名称:")
    price_of_product = input("请输入你要添加商品的价格:")
    f = open("porduct.txt","a",encoding="utf-8")
    f.write(str("\n"+name_of_product)+": %s"%(price_of_product))
    f.close()
    print_product_list()
    print("添加成功!\n exit------")

# add_product() #添加商品

def change_price():
    print_product_list() #
    choice = input("请输入你的选择:")
    if choice.isdigit():
        choice = int(choice)
        if choice >= 0 and choice <= len(product_list):
            c_item = product_list[choice]
            print(c_item)
            change_item = input("请输入要修改的,货品和价格用逗号分隔")
            change_item = str(change_item).split(",")
#            print(change_item[0])
#            print(change_item[1])
            for line in fileinput.input("product.txt",backup=‘.bak‘,inplace=1):
                line = line.replace("%s" %(p))

    # name_of_change = input("请输入你要改商品的名称:")
    # price_of_change = input("请输入你要改商品的价格:")
#     if choice.isdigit():
#         choice = int(choice)
#         if choice >= 0 and choice <= len(product_list):
#             p_item = product_list[choice]
# #            print(p_item) #看一下格式
#             for line in fileinput.input("product.txt",inplace = "%s" %(choice)):
#                 line = line.replace("%s" %(p_item[1]),"%s" %(price_of_change)).strip()
#                 print(line)
#             exit("修改成功!")
#         else:
#             print("输入无效!")
#     else:
#         if choice == "q" or choice == "Q":
#             exit("退出")

#change_price()

  没搞明白fileinput模块的inplace!

原文地址:https://www.cnblogs.com/armyz6666666/p/9038506.html

时间: 2024-08-30 06:54:34

没搞明白fileinput模块的inplace!的相关文章

利用NativeWindow监视WndProc消息(好像是一个字典,没搞明白)

http://blog.csdn.net/lovefootball/article/details/1784882 在写Windows应用程序的时候,经常会碰到需要修改例如MessageBox或者FileDialog的外观此时我们需要监视 WndProc的消息当然也可以直接调用API实现,具体方法请参考http://www.codeproject.com/csharp/GetSaveFileName.asp?df=100&forumid=96342&exp=0&select=195

【python之路】【之前没搞明白】5面向对象(多态)

多态 有时一个对象会有多种表现形式,比如网站页面有个button按钮, 这个button的设计可以不一样(单选框.多选框.圆角的点击按钮.直角的点击按钮等),尽管长的不一样,但它们都有一个共同调用方式,就是onClick()方法.我们直要在页面上一点击就会触发这个方法.点完后有的按钮会变成选中状态.有的会提交表单.有的甚至会弹窗.这种多个对象共用同一个接口,又表现的形态不一样的现象,就叫做多态( Polymorphism ). Polymorphism is based on the greek

【python之路】【之前没搞明白的】6面向对象(编程思路及步骤)

思路 设计功能——使用功能 步骤 光学会了class的语法还不足以让我们学会怎么去编程.就像学会了和水泥.垒砖.刮腻子 但是却不知道怎么去盖楼一样.有个顺序: 1. 定模型 多少个类2. 定属性 每个类需要哪些属性,self.name 这样的3. 定关系 每个类之间有什么关系,继承.依赖.组合.关联4. 画XML图 https://www.processon.com/5. 写代码 这是不是架构师的主要工作! 原文地址:https://www.cnblogs.com/watalo/p/123299

【python之路】【之前没搞明白】8面向对象(反射,非常重要)

反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩. 一句话解释:通过字符串的形式操作对象相关的属性 四个函数: 方法 作用 hasattr(*args, **kwargs) 返回对象里面是否有这个属性 *args:目标实例.对象.eg:p, Person **kwargs:属性的名字,以字符串形

poj3687 拓扑排序 还没怎么搞明白 回头再想想

[题意]:n个重量为1~n的球,给定一些球之间的重量比较关系(如 2 1  表示第二个球比第一个球轻),求每个球可能的重量,ans[i] 表示第i个球的重量,要求输出的是ans字典序最小的情况. [思路]:对于给出的a b  建反边,每次 在出度为0的所有点里选一个序号最小的赋值(从n开始 由大到小赋). 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string>

容器公司拿到5千万A+轮,搞明白了传统企业生意经

(上图为数人云创始人及CEO王璞) 2016年11月,中国开源云容器工作组发布了<容器技术及其应用白皮书 V1.0>.<白皮书>指出,继虚拟化技术出现后,容器技术逐渐成为对云计算领域具有深远影响的变革技术.容器技术的发展和应用,为各行业应用云计算提供了新思路,同时容器技术也将对云计算的交付方式.效率.PaaS平台的构建等方面产生深远影响. 容器技术从2014年开始风靡全球,美国以 2013年Docker公司成立为标志.中国以2014年底的一批Docker容器公司为标志,出现了一波D

Python中fileinput模块介绍

fileinput模块可以对一个或多个文件中的内容进行迭代.遍历等操作. 该模块的input()函数有点类似文件readlines()方法,区别在于: 前者是一个迭代对象,即每次只生成一行,需要用for循环迭代. 后者是一次性读取所有行.在碰到大文件的读取时,前者无疑效率更高效. 用fileinput对文件进行循环遍历,格式化输出,查找.替换等操作,非常方便. [典型用法] import fileinput for line in fileinput.input(): process(line)

fileinput模块

刚才练习的时候,报如下错误: AttributeError: module 'fileinput' has no attribute 'input',后来Google参考这篇文章https://mail.python.org/pipermail/tutor/2005-June/039321.html发现原来文件名写成fileinput.py了,这样你导入的就不是fileinput这个模块了,而是你的程序,所以就报没有input属性,把程序文件名改成别的就可以了. #!/usr/bin/env p

fileinput模块可以循环一个或多个文本文件的内容

fileinput模块可以循环一个或多个文本文件的内容. [默认格式] fileinput.input (files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None) 1 2 3 4 5 6 files:                  #文件的路径列表,默认是stdin方式,多文件['1.txt','2.txt',...] inplace:                #是否将标准输出的结果写回文件,默认