Python新手,写一个文本文件

时间:2014-09-01 16:38:58

标签: python python-2.7

我是编程新手。我正在尝试创建一个程序,我可以在其中键入客户端的详细信息,它在文件中提供文本输出。一切都按计划工作,除了当我尝试在这一行添加EndDate时它给我一个错误:

[EndDate+','+Email+','+PassWord+','+i+'\n'+Cell+','+Name+Message1+','+Name]
text_file.writelines(lines))

这是我的计划。

from datetime import datetime
i=str(raw_input('StartingDate: '))
try:
dt_start=datetime.strptime(i,'%Y%m%d')


except ValueError:
print'Incorrect Format'

from datetime import timedelta
EndDate = dt_start+timedelta(days=1)
print EndDate



Name = raw_input ('Name: ')
SurName = raw_input ('SurName: ')
Cell = raw_input ('CellNo: ')
EmailAdd = raw_input ('Email: ')

Time = raw_input ('MessageTime: ')
Blank = raw_input ('------------- ')


Email = 'douwecoertze1@gmail.com'
PassWord ='oxoxoxox7'
x = 'Hallo '
Message1 = ',lekker dag'
Message2 = ' enjoy'


text_file = open ("write_it1.txt", "w")
lines = [EndDate+','+Email+','+PassWord+','+i+'\n'+Cell+','+Name+Message1+','+Name]
text_file.writelines(lines)
text_file.close()

text_file = open ("write_it2.txt", "w")
lines = [Email+','+PassWord+','+i+'\n'+Cell+','+Name+Message2+','+Name]
text_file.writelines(lines)
text_file.close()

text_file = open("write_it3.txt", "w")
lines = [Name+','+SurName+'\n'+Time+'\n'+i+'\n'+Cell+'\n'+EmailAdd+'\n'+Blank]
text_file.writelines(lines)
text_file.close()


text_file = open("write_it3.txt", "r")
print text_file.read()
text_file.close()

text_file = open("write_itAll.txt", "a")
lines = [Name+','+SurName+'\n'+Time+'\n'+i+'\n'+Cell+'\n'+EmailAdd+'\n'+Blank]
text_file.writelines(lines)
text_file.close()

raw_input("Press<enter>")

为什么会出现此错误?我该如何解决?

1 个答案:

答案 0 :(得分:0)

我猜错误是 - +不支持的操作数类型+:'datetime.datetime'和'str'

就像下面的str(EndDate)一样,它应该可以工作。

[str(EndDate)+','+Email+','+PassWord+','+i+'\n'+Cell+','+Name+Message1+','+Name]
text_file.writelines(lines))

如果有帮助请告诉我