使用输入变量到CSV文件

时间:2018-10-26 19:36:32

标签: python csv

试图将用户输入保存在csvcolumns下,如下所示,但是每个输入都保存在自己的行中。 示例:如果在名称变量下输入John,则将其另存为j                                                                         Ø                                                                         H                                                                         ñ 下面是代码。

import time
import csv
csvcolumns= [ "NAME", "CAR MAKE", "YEAR OF MANUFACTURE", "IDENTIFICATION     NUMBER", "DATE"]
Name= input("Enter your name: ")
Car_make= input("Enter your car make e.g Toyota probox: ")
YOM= input("Enter the car YOM: ")
ID= input("Enter your identification number details: ")
todays_date= time.strftime("%Y-%m-%d %H:%M:%S")
with open('E:\doo.csv', 'w') as csvFile:
  writer=csv.writer(csvFile, delimiter=',')
  writer.writerow(csvcolumns)
  writer.writerows(zip(Name, Car_make,YOM,ID,todays_date))


print("writing completed")

1 个答案:

答案 0 :(得分:2)

您迷失在复杂事物的某个地方。

如果所有这些数据都是单行,则用

writer.writerow([Name, Car_make, YOM, ID, todays_date])