2014-09-01 88 views
-3

我是編程新手。我正在嘗試創建一個程序,可以輸入客戶端的詳細信息,並可以在文件中輸出文本。一切都按計劃運作,只是當我嘗試在此行中添加EndDate時出現錯誤:Python新手,寫一個文本文件

[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 = '[email protected]' 
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>") 

爲什麼我得到這個錯誤?我該如何解決它?

+2

你有沒有聽說過的循環? – 2014-09-01 16:53:08

+1

你的第一篇文章有​​幾個問題。首先,代碼格式不正確,難以遵循。接下來,您詢問有關錯誤的信息,並且不要說明錯誤。最後,該示例太長http://stackoverflow.com/help/mcve。嘗試清理它,然後我們可以幫助 – user590028 2014-09-01 16:53:19

+2

給我們的錯誤會很好!任何機會「TypeError:不支持的操作數類型爲+:'datetime.timedelta'和'str'」 – tdelaney 2014-09-01 16:54:28

回答

0

我猜的錯誤是 - 不支持的操作數類型(S)爲+: 'datetime.datetime' 和 '海峽'

只是做STR(結束日期),如下面的,它應該工作。

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

讓我知道這是否有助於