首页 > 解决方案 > 根据用户输入构建 .csv

问题描述

我需要根据用户输入添加到 .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 文件之外,代码的早期部分功能完美。这是为了填补缺失的数据,否则这些数据不会在单独的数据库中列出。

标签: pythoncsvinput

解决方案


对代码进行了一些改进。

  1. 您可以使用循环条件,例如whileorfor代替递归
  2. 您可以在代码开头打开 csv 文件,而不是每次都这样做
  3. 你可以想出一句话stop来停止循环,关闭文件并退出
  4. 您可以使用str.lower() == 'y'来覆盖yand Y,大写和小写

然后代码看起来像

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()

推荐阅读