2017-08-30 60 views
-2

用戶將在以下格式Python解釋輸入數據: -從python解釋以輸入和寫入到文件

Chapters 
TRAN~Transfiguration~1~2011-2012~Instructor1 
CHAR~Charms~1~2011-2012~Instructor2 
Students 
001~Hannah Abbott 
002~Euan Abercrombie 
003~Stewart Ackerley 
004~Bertram Aubrey 
005~Avery 
006~Malcolm Baddock 
007~Marcus Belby 
008~Katie Bell 
009~Sirius Orion Black 
Grades 
subject1~1~2011-2012~SLY2301~AB 
subject2~2011-2012~SLY2302~B 
TRAN~1~2011-2012~SLY2303~B 
TRAN~1~2011-2012~SLY2305~A 

和數據已被寫入到用於進一步處理的文件。如何在Python中做到這一點?

+2

你看過輸入()函數嗎? https://docs.python.org/3/library/functions.html#input另外看看寫入文件的'open.write'函數。 – numbermaniac

回答

0
entry = "Anything"; 

with open("file.txt", 'w') as f: 
    while entry != "Done": 
     entry = input("Enter here: "); 
     f.writelines(entry+"\n"); 

希望這會有所幫助。

P.S:嘗試提供一些嘗試,以解決問題或問題。這會鼓勵人們來幫助你。

+0

順便提一下,這些行末尾不需要分號。這不是JavaScript :) – numbermaniac

+1

是啊@numbermaniac。我從我的C背景作爲練習的一部分。它也避免了我的行結束/語法混淆^ _^ – RussellB

+0

我使用了上面的代碼,但是我得到的是語法錯誤 回溯(最近調用最後一次): 文件「C:\ Python27 \ readafile.py」 ,第5行,在 entry = input(「Enter here:」); 文件「」,第2行 TRAN〜Transfiguration〜1〜2011-2012〜Instructor1 ^ SyntaxError:無效的語法 –