【发布时间】:2016-05-03 04:28:47
【问题描述】:
我想将数据输入到 CSV 文件中,并且到目前为止已经完成。我现在遇到的唯一问题是,当用户将错误信息输入到仅输入字符串(例如 name = b12f)时,程序成功提示用户再次仅输入字符串(例如 name = biff)。
但是,当我查看 CSV 文件时,记录的值不正确,而不是更正后的输入。我该如何纠正这个问题?
import csv
surname = input('\nSurname:')
surname_checker(surname) #I wrote a seperate function that correctly checks the input to judge if it is correct and reprompt if not
s = surname.capitalize()
with open('Try.csv','a',newline='') as csvfile:
appendCSV = csv.writer(csvfile)
appendCSV.writerow([s])
csvfile.close()
如果输入 b12f 则 Biff 正确输入的结果:
Surname:b12f
Please define Surname only in letters
Surname:biff
>>> print(surname)
b12f
【问题讨论】:
标签: python-3.5