Python 3: receive user input including newline characters

后端 未结 2 542
迷失自我
迷失自我 2020-12-18 05:26

I\'m trying to read in the following text from the command-line in Python 3 (copied verbatim, newlines and all):

lcbeika
rraobmlo
grmfina
ontccep
emrlin
tsei         


        
相关标签:
2条回答
  • 2020-12-18 05:52

    You can import sys and use the methods on sys.stdin for example:

    text = sys.stdin.read()
    

    or:

    lines = sys.stdin.readlines()
    

    or:

    for line in sys.stdin:
        # Do something with line.
    
    0 讨论(0)
  • 2020-12-18 05:58

    if you are passing the text into your script as a file , you can use readlines()

    eg

    data=open("file").readlines()
    

    or you can use fileinput

    import fileinput
    for line in fileinput.input():
        print line
    
    0 讨论(0)
提交回复
热议问题