Read string of several lines entered in PYTHON console?

2

I use Python 3.6 and I try to read an entry data of several lines to store it in a variable and then manage each line in a list for example. The structure of the code is something simple like this:

    print("Escriba la lista de ciudades de la siguiente forma:")
    print("Ciudad origen | Ciudad destino | Ruta | Distancia")
    cad.append(input())  

I want to enter 3 lines for example (without pressing ENTER, copying and pasting for example)

  
    

Line 1
    Line 2
    Line 3
    And I want python to store the 3 lines in a variable

  
    
asked by Parzival 30.03.2017 в 20:24
source

1 answer

1

Input (python 3.x) returns a string, to divide each element you can use the method split of the strings using the appropriate separator, in your case | :

print("Escriba la lista de ciudades de la siguiente forma:")
print("Ciudad origen | Ciudad destino | Ruta | Distancia")
cadd = input().split('|')

Edit: Ok, what you want for what it looks like is a multiline input, input only reads until you come across a end of line ('\ n', '\ r', ...). A simple way would be to use a cycle% co_of infinite%. To stop the entry you can specify anything, in this case entering a blank line indicates that the user ends the data entry:

print("Escriba la lista de ciudades de la siguiente forma:")
print("Ciudad origen | Ciudad destino | Ruta | Distancia")

lista = []
while True:
    inputs = input()
    if inputs:
        lista.append(inputs)
    else:
        break
print(lista)

In this case you can copy and paste a text with several lines in the console and finally each line will be an element of the list.

    
answered by 30.03.2017 / 20:29
source