python - 根据用户输入构建 .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 文件之外,代码的早期部分功能完美。这是为了填补缺失的数据,否则这些数据不会在单独的数据库中列出。
解决方案
对代码进行了一些改进。
- 您可以使用循环条件,例如
while
orfor
代替递归 - 您可以在代码开头打开 csv 文件,而不是每次都这样做
- 你可以想出一句话
stop
来停止循环,关闭文件并退出 - 您可以使用
str.lower() == 'y'
来覆盖y
andY
,大写和小写
然后代码看起来像
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()
推荐阅读
- flutter - 单击按钮时显示小部件的错误文本
- spring-cloud-feign - OAuth2认证的feign客户端不支持异步调用?
- django - 发送 post 请求后删除数据表
- java - Servlet 请求的 Java 并发问题
- node.js - 如何配置时刻不显示:弃用警告:提供的值不是可识别的 RFC2822 或 ISO 格式
- oracle - 如何在 NIFI 的 Oracle 的 DBCPConnectionPool 中设置 useUnicode=yes& characterEncoding=UTF-8?
- python - 在数据库(sqlite)模型之间创建关系的选项。烧瓶
- excel - 将多行合并为一行,同时将其列值转换为单行
- python - 连接到容器内运行的 OPC UA 服务器
- kubernetes - 如何修复 kubernetes 控制器管理器探测失败:HTTP 探测失败,状态码:400