我正在用python寫一個簡單的日誌程序。我使用sys.stdin.readlines()
,所以用戶可以按Enter鍵開始一個新段落,而不必編程退出。問題在於,當它將輸入寫入文本文件時,它會跳過它們輸入的第一段。所以任何他們在開始新行之前寫入的內容都不會寫入文件。Python writelines跳過raw_input的第一行
#!/usr/bin/python
import sys
def main(): #begin stand alone program
print "\nPlease tell me about your day.\n";
userEntry = raw_input() #assigns user's input
todayEntry = userEntry
todayEntry = sys.stdin.readlines()
print "\nThank you! See you tomorrow!\n"
with open('journal.txt', 'a') as f:
f.writelines(todayEntry) #write user's input to txt file
if __name__ == '__main__':
main() #call to main() and complete program
'userEntry =的raw_input()'讀取一行,然後分配它到'todayEntry',然後丟棄這些數據並用'readlines()'結果覆蓋它。 基本上,'raw_input()'是不需要的。 –
基本上,刪除'sys.stdin.realines()'行。這似乎沒有太大的目的。如果你想閱讀多個段落,你可能想在'while'中使用'raw_input',在特殊命令中使用'break' – inspectorG4dget