【问题标题】:Building a .csv based on user input根据用户输入构建 .csv
【发布时间】:2019-05-04 06:16:23
【问题描述】:

我需要根据用户输入添加到 .csv 文件。代码的其他部分添加到文件中,但我不知道如何让它添加用户输入。我是 python 和一般编码的新手。

我有代码的其他部分可以合并或从 .csv 数据库中提取数据并将其写入单独的文件,但不知道如何让它接受多个用户输入来写入或附加到传出文件。

def manualentry():
        stock = input("Enter Stock #: ") #Generate data for each column to fill in to the output file.
        VIN = input("Enter Full VIN: ") #Each line asks the user to add data do the line.
        make = input("Enter Make: ")
        model = input("Enter Model: ")
        year = input("Enter Year: ")
        l8v = input("Enter L8V: ")
        print(stock, VIN, make, model, year, l8v) #Prints the line of user data
        input4 = input("Append to inventory list? Y/N") #Asks user to append the data to the output file.
        if input4 == "Y" or input4 == "y":
            with open('INV.csv','a', newline='') as outfile: #Pull up a seperate csv to write to, an output for collected data
                w = csv.writer(outfile) #Need to write the user input to the .csv file.
                w.writerow([stock, VIN, make, model, year, l8v]) #<-This is the portion that seems to fall apart.
                print("INVENTORY UPDATED")
                starter() #Restarts whole program from begining.
        if input4 == "N" or input4 == "n":
            print("SKIPPING. RESTARTING....")            
            starter() #Reset
        else:
            print("Invalid entry restarting program.")
            starter() #Reset
starter() #R E S E T !

只需要将用户输入应用到 .csv 并保存在那里。除了要添加到 .csv 文件之外,代码的早期部分功能完美。这是为了填补缺失的数据,否则这些数据不会在单独的数据库中列出。

【问题讨论】:

    标签: python csv input


    【解决方案1】:

    对代码进行了一些改进。

    1. 您可以使用 whilefor 之类的循环条件来代替递归
    2. 您可以在代码开头打开 csv 文件,而不是每次都打开
    3. 你可以想出一个词说stop来停止循环,关闭文件并退出
    4. 您可以使用str.lower() == 'y' 覆盖yY,大写和小写

    然后代码将如下所示

    import csv
    
    def manualentry():
    
        #Open csv file at start
        outfile = open('INV.csv', 'a', newline='')
        w = csv.writer(outfile)  # Need to write the user input to the .csv file.
    
        #Everything wrapped in a while True loop, you can change to any loop accordingly
        while True:
            stock = input("Enter Stock #: ")  # Generate data for each column to fill in to the output file.
            VIN = input("Enter Full VIN: ")  # Each line asks the user to add data do the line.
            make = input("Enter Make: ")
            model = input("Enter Model: ")
            year = input("Enter Year: ")
            l8v = input("Enter L8V: ")
            print(stock, VIN, make, model, year, l8v)  # Prints the line of user data
            input4 = input("Append to inventory list? Y/N")  # Asks user to append the data to the output file.
            if input4.lower() == "y":
                w.writerow([stock, VIN, make, model, year, l8v])  # <-This is the portion that seems to fall apart.
                print("INVENTORY UPDATED")
            if input4.lower() == "n":
                print("SKIPPING. RESTARTING....")
            #If you see stop, stop writing, close the file and exit
            if input4.lower() == 'stop':
                print('Not writing anymore! Stopping')
                outfile.close()
                exit()
            else:
                print("Invalid entry restarting program.")
    
    
    #Call manualentry
    manualentry()
    

    【讨论】:

    • 这只是代码的一个小sn-p,我觉得将其余代码包含在我正在尝试弄清楚的上下文中是不明智的。我剪掉了另外 2/3 来展示代码中似乎不起作用的部分。把它全部展示出来会有帮助吗?
    • 所以我假设你知道循环 :D 因为从这篇文章中感觉你不能要求用户重新输入所以我添加了一个循环,请添加完整部分
    • 我在使用标准 .csv 模块将这部分代码写入 .csv 时遇到问题。我已经编辑了主帖子以显示它退出了无限循环,并且只有在程序的上部无法从另一个数据库中提取数据时才可以访问它。本节应该将缺失的数据写入 .csv。
    • 我已经更新了代码,在这里和那里做了一些小改动,看看@BrandonToolbelt,如果你有任何疑问,请告诉我
    【解决方案2】:

    您可以简单地使用用户输入控制的while循环来递归地获取用户输入,然后您可以根据用户的选择退出

    user_input = 'Y'
    while user_input.lower() == 'y':
        # Run your code here
        user_input = input('Do you want to add one more entry: Enter [Y/N]')
    

    【讨论】:

    • 我将在程序的未来版本中实现这一点,我感谢你的这个 sn-p,但这对问题的一般意义没有帮助。我无法获取这部分代码来获取用户输入并将它们添加到 .csv 文件中。
    【解决方案3】:

    试试这个

    import csv
    
    
    def boo():
       stock = input("Enter Stock #: ")  # Generate data for each column to fill in  to the output file.
       VIN = input("Enter Full VIN: ")  # Each line asks the user to add data do the line.
       make = input("Enter Make: ")
       model = input("Enter Model: ")
       year = input("Enter Year: ")
       l8v = input("Enter L8V: ")
       print(stock, VIN, make, model, year, l8v)  # Prints the line of user data
    
       input4 = input(
        "Append to inventory list? Y/N  || anything else to exit")  # Asks user to append the data to the output file.
    
       if input4 == "Y" or input4 == "y":
           with open('INV.csv', 'a',
                  newline='') as outfile:  # Pull up a separate csv to write to, an output for collected data
    
              w = csv.writer(outfile)
              w.writerow([stock, VIN, make, model, year,
                        l8v])  # Need to write the previously pulled up line to new csv
              print("INVENTORY UPDATED")
              user_input = input('Do you want to add one more entry: Enter [Y/N]')
              if user_input.lower() == 'y':
                  boo()
              else:
                  exit()
    boo()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 2018-03-12
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多