Skip to content Skip to sidebar Skip to footer

Python Writelines Skips First Line Of Raw_input

I am writing a simple journal program with python. I used sys.stdin.readlines() so the user could press enter to start a new paragraph and not have to program quit. The problem is

Solution 1:

You're reading input with userEntry = raw_input(), so userEntry now contains the first line the user enters (because that's what raw_input() does). You're then reading more input with todayEntry = sys.stdin.readlines(). todayEntry now contains whatever else the user enters (returned from sys.stdin.readlines()). You're then writing todayEntry to a file, so the file contains what the user entered after the first line.

Solution 2:

You can do something like this:

import sys

todayEntry = ""print"Enter text (press ctrl+D at the last empty line or enter 'EOF'):"whileTrue:
    line = sys.stdin.readline()

    ifnot line or line.strip()=="EOF":
        break

    todayEntry += line

print"todayEntry:\n"print todayEntry

sample input:

111

222

333
ctrl+D

Output:

111

222

333

Post a Comment for "Python Writelines Skips First Line Of Raw_input"