【发布时间】: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 文件之外,代码的早期部分功能完美。这是为了填补缺失的数据,否则这些数据不会在单独的数据库中列出。
【问题讨论】: